How to create a constructor for a custom UIButton class? - iphone

I have created a subclass of the UIButton class. I want to change its background color upon the creation. How do I write the constructor method?

-(id)initWithFrame :(CGRect)frame{
if(self = [super init]) {
// The default size for the save button is 49x30 pixels
self.frame = frame;//CGRectMake(0, 0, 60.0, 34.0);
// Center the text vertically and horizontally
self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
self.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
UIImage *image = [UIImage imageNamed:#"image.png"];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setBackgroundColor:[UIColor blueColor]];
// Set the font properties
// [self setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
self.titleLabel.font = [UIFont boldSystemFontOfSize:12];
}
return self;
}

Related

UIToolbar is getting hidden

I have created UIToolbar control.which is added as setInputAccessoryView to one UITextField Control.Which works fine. the tollbar comes up with uikeyboard when tapping UITextField.Up to this no issues.
When i placed one button, when tapping on it i want to hide the keyboard and only want to display the toolbar.
This is not happening.the toolbar is getting hidden.
Please let me know , what is the problem/ reason in the code.
Thanks for your time.
- (void)viewDidLoad {
[super viewDidLoad];
//
myToolbar = [[UIToolbar alloc] init];
[myToolbar sizeToFit];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
UIImage *image = [UIImage imageNamed:#"orangebuttonsmall.png"];
UIButton *aButton = [UIButton buttonWithType:UIButtonTypeCustom];
aButton.frame = CGRectMake(0, 100, image.size.width, 25 );
aButton.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[aButton setTitle:#"Tap" forState:UIControlStateNormal];
[aButton setBackgroundImage: image forState:UIControlStateNormal];
[aButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
aButton.titleLabel.font = [UIFont boldSystemFontOfSize:10];
[aButton addTarget:self action:#selector(aCenterBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:aButton];
myToField = [[UITextField alloc] init];
myToField.frame = CGRectMake(0, 0, 320, 48);
myToField.textColor = [UIColor blackColor]; //text color
myToField.font = [UIFont systemFontOfSize:17.0]; //font size
myToField.placeholder = #"To:"; //place holder
myToField.backgroundColor = [UIColor whiteColor]; //background color
myToField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
myToField.keyboardType = UIKeyboardTypePhonePad; // type of the keyboard
myToField.returnKeyType = UIReturnKeyDone; // type of the return key
myToField.clearButtonMode = UITextFieldViewModeWhileEditing;
[myToField setInputAccessoryView:myToolbar];
myToField.textAlignment = UITextAlignmentLeft;
[myToField setContentVerticalAlignment:UIControlContentVerticalAlignmentCenter];
myToField.rightViewMode=UITextFieldViewModeAlways;
// has a clear 'x' button to the right
myToField.delegate = self;
[myToField becomeFirstResponder];
[self.view addSubview:myToField];
}
-(void)aCenterBtnClicked:(id)sender{
[myToField resignFirstResponder];
myToolbar.frame = CGRectMake(0,198, 320, 35);
myToolbar.barStyle = UIBarStyleBlackTranslucent;
[self.view addSubview:myToolbar];
}
inputAccessoryViews are placed as a subview on the UIKeyboard, if you hide the keyboard, it will hide the inputAccessoryView. Try adding the toolbar as a subview of self.view and animate it's position up when the keyboard is displayed by subscribing to the UIKeyboardWillShow notification and pulling the animation duration from the userInfo.
You try for static toolbar i think that will be easy for you.I am not done before remove from setinputview because there is no method for remove set input view.

subclass UIView actions in viewcontroller

How do I go about adding actions to buttons that are created in a UIView subclass?
Here is my code. This is my first time writing my views purely programmatically in a UIView subclass. I have create the "directions" button as follows:
directionsButton = [self buttonWithTitle:#"Directions" target:self selector:#selector(getDirections:) frame:directionsFrame image:buttonImage insertImage:directionsIcon];
Now how can I add the "getDirections" selector in my view controller? The following UIView subclass is added as a subview of BookViewController
#import "BookView.h"
#define GRAY_BOTTOM 44
#define PADDING 5
#define THUMB_WIDTH 50
#define THUMB_HEIGHT 43
#define DIRECTIONS_BUTTON_WIDTH 94
#define BUTTON_HEIGHT 34
#define BUTTON_VERTICAL_INSET 2
static NSArray* grayBottomButtons = nil;
#interface BookView (PrivateMethods)
- (void)setupSubviewsWithContentFrame:(CGRect)frameRect;
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image insertImage:(UIImage*)insertImage;
#end
#implementation BookView
// #synthesize delegate;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self setupSubviewsWithContentFrame:frame];
self.backgroundColor = [UIColor clearColor]; // Make sure clear Background
self.opaque = NO;
}
return self;
}
- (void)setupSubviewsWithContentFrame:(CGRect)frameRect {
// Setup views
// Create frame for white background
CGRect frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
WhiteRoundedView *whiteBg = [[WhiteRoundedView alloc] initWithFrame:frame];
// Create frame for gray bottom view
CGRect gFrame = CGRectMake(0, (self.frame.size.height - GRAY_BOTTOM), self.frame.size.width, GRAY_BOTTOM);
GrayBottomView *grayBg = [[GrayBottomView alloc] initWithFrame:gFrame];
// Create salon thumbnail view
UIImageView *salonThumb = [[UIImageView alloc] initWithFrame:CGRectMake(PADDING, PADDING, THUMB_WIDTH, THUMB_HEIGHT)];
// Set salonThumb image
salonThumb.image = [UIImage imageNamed:#"SalonThumb.png"];
// Set salonName UILabel
UILabel *salonName = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), PADDING, 200, 15)];
// Set text
salonName.text = #"Salon Aria";
// Set font
salonName.font = [UIFont fontWithName:#"PTSans-Bold" size:15.0];
// Set Alignment
salonName.textAlignment = UITextAlignmentLeft;
// Set font color
salonName.textColor = [UIColor colorWithRed:0.0f/255.0 green:0.0f/255.0 blue:0.0f/255.0 alpha:1.0];
// Set salonAddressLineOne UILabel (Line One)
UILabel *salonAddressLineOne = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), (PADDING + 15), 140, 13)];
// Set text
salonAddressLineOne.text = #"6325 Main St, Suite 140";
// Set font
salonAddressLineOne.font = [UIFont fontWithName:#"PTSans-Regular" size:13.0];
// Set Alignment
salonAddressLineOne.textAlignment = UITextAlignmentLeft;
// Set font color
salonAddressLineOne.textColor = [UIColor colorWithRed:40.0f/255.0 green:40.0f/255.0 blue:40.0f/255.0 alpha:1.0];
// Set salonAddressLineTwo UILabel (Line Two)
UILabel *salonAddressLineTwo = [[UILabel alloc] initWithFrame:CGRectMake((PADDING*2 + THUMB_WIDTH), (PADDING + 28), 140, 13)];
// Set text
salonAddressLineTwo.text = #"Woodridge, IL 60517";
// Set font
salonAddressLineTwo.font = [UIFont fontWithName:#"PTSans-Regular" size:13.0];
// Set font color
salonAddressLineTwo.textColor = [UIColor colorWithRed:40.0f/255.0 green:40.0f/255.0 blue:40.0f/255.0 alpha:1.0];
// Set Alignment
salonAddressLineTwo.textAlignment = UITextAlignmentLeft;
// Set the background image with stretchable type for all buttons on this view
UIImage *buttonImage = [[UIImage imageNamed:#"UIButton.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
// Set the insert image (directions icon)
UIImage *directionsIcon = [[UIImage imageNamed:#"DirectionsIcon.png"] stretchableImageWithLeftCapWidth:2 topCapHeight:2];
// Set the directions button frame
CGRect directionsFrame = CGRectMake(self.frame.size.width - PADDING - DIRECTIONS_BUTTON_WIDTH, PADDING, DIRECTIONS_BUTTON_WIDTH, BUTTON_HEIGHT);
// Add the button
directionsButton = [self buttonWithTitle:#"Directions" target:self selector:#selector(getDirections:) frame:directionsFrame image:buttonImage insertImage:directionsIcon];
// Create the salonMap with frame
salonMap = [[MKMapView alloc] initWithFrame:
CGRectMake(PADDING, (PADDING*2 + THUMB_HEIGHT), (self.frame.size.width - PADDING*2), (self.frame.size.height - THUMB_HEIGHT - PADDING*3 - GRAY_BOTTOM))];
// Add view in proper order and location
[self addSubview:whiteBg];
[whiteBg addSubview:grayBg];
[whiteBg addSubview:salonThumb];
[whiteBg addSubview:salonName];
[whiteBg addSubview:salonAddressLineOne];
[whiteBg addSubview:salonAddressLineTwo];
[whiteBg addSubview:directionsButton];
[whiteBg addSubview:salonMap];
[self setNeedsDisplay];
}
- (UIButton *)buttonWithTitle:(NSString *)title target:(id)target selector:(SEL)inSelector frame:(CGRect)frame image:(UIImage*)image insertImage:(UIImage*)insertImage
{
// Create button
UIButton *button = [[UIButton alloc] initWithFrame:frame];
// Set button content alignment
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
// Set button title
[button setTitle:title forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
// Set button title color
[button setTitleColor:[UIColor colorWithRed:91.0f/255.0 green:82.0f/255.0 blue:82.0f/255.0 alpha:1.0] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
// Add the background image
[button setBackgroundImage:image forState:UIControlStateNormal];
// Add Events
[button addTarget:target action:inSelector forControlEvents:UIControlEventTouchUpInside];
// in case the parent view draws with a custom color or gradient, use a transparent color
[button setBackgroundColor:[UIColor clearColor]];
// Set titleShadowColor this way (apparently, titleLabel.shadowcolor does not work)
[button setTitleShadowColor:[UIColor colorWithRed:255.0f/255.0 green:255.0f/255.0 blue:255.0f/255.0 alpha:.75] forState:UIControlStateNormal & UIControlStateHighlighted & UIControlStateSelected];
// Set button titleLabel properties
button.titleLabel.font = [UIFont fontWithName:#"PTSans-Bold" size:13.0];
button.titleLabel.shadowOffset = CGSizeMake(1, 1);
// If there is an insertImage icon
if(insertImage != nil)
{
// padding between image and text
int seperate = 6;
// Image and title frame (used to center text/image)
int width = (button.titleLabel.frame.size.width + insertImage.size.width + seperate);
int buttonWidth = button.frame.size.width;
int xPadding = (buttonWidth - width)/2;
int yPadding = (button.frame.size.height - insertImage.size.height)/2;
// Create an image view for the image (standard icon is 12x13)
UIImageView *insertImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xPadding, yPadding, insertImage.size.width, insertImage.size.height)];
// add the insertImage to the view
[insertImageView setImage:insertImage];
// add the insertImageView to the button
[button addSubview:insertImageView];
// Add offset with title
button.titleEdgeInsets = UIEdgeInsetsMake(BUTTON_VERTICAL_INSET, insertImage.size.width + seperate, 0, 0);
}
else
{
// No image so no horizontal offset but PTSans font needs a 2 vertical offset
button.titleEdgeInsets = UIEdgeInsetsMake(BUTTON_VERTICAL_INSET, 0, 0, 0);
}
return button;
}
#end
From the view controller, for each button in the view do something like:
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
Alternatively you can set up the connection within Interface Builder by ctrl-dragging from the button to the header file of the file owner (the view controller). This will ask you to define a method name, and link it all up for you.

Customization of UINavigationBar and the back button

I followed the tutorial below to customize the UINavigationBar.
http://foobarpig.com/iphone/uinavigationbar-with-solid-color-or-image-background.html
I applied a background image in the UINavigationBar, however, I do not know how to customize the back button. At the moment, the default back button does not suits the look & feel of the customized UINavigationBar.
Please teach me how to change the background color or image of the default back button. Thank you.
I've written the following categories to customize the back button:
UIBarButtonItem+StyledButton.h
#interface UIBarButtonItem (StyledButton)
+ (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)styledCancelBarButtonItemWithTarget:(id)target selector:(SEL)selector;
+ (UIBarButtonItem *)styledSubmitBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
#end
UIBarButtonItem+StyledButton.m
#implementation UIBarButtonItem (StyledButton)
+ (UIBarButtonItem *)styledBackBarButtonItemWithTarget:(id)target selector:(SEL)selector;
{
UIImage *image = [UIImage imageNamed:#"button_back"];
image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
NSString *title = NSLocalizedString(#"Back", nil);
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
button.titleLabel.textColor = [UIColor blackColor];
CGSize textSize = [title sizeWithFont:font];
CGFloat margin = (button.frame.size.height - textSize.height) / 2;
CGFloat marginRight = 7.0f;
CGFloat marginLeft = button.frame.size.width - textSize.width - marginRight;
[button setTitleEdgeInsets:UIEdgeInsetsMake(margin, marginLeft, margin, marginRight)];
[button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
+ (UIBarButtonItem *)styledCancelBarButtonItemWithTarget:(id)target selector:(SEL)selector;
{
UIImage *image = [UIImage imageNamed:#"button_square"];
image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
NSString *title = NSLocalizedString(#"Cancel", nil);
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
button.titleLabel.textColor = [UIColor blackColor];
[button setTitleColor:[UIColor colorWithRed:53.0f/255.0f green:77.0f/255.0f blue:99.0f/255.0f alpha:1.0f] forState:UIControlStateNormal];
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
+ (UIBarButtonItem *)styledSubmitBarButtonItemWithTitle:(NSString *)title target:(id)target selector:(SEL)selector;
{
UIImage *image = [UIImage imageNamed:#"button_submit"];
image = [image stretchableImageWithLeftCapWidth:20.0f topCapHeight:20.0f];
UIFont *font = [UIFont boldSystemFontOfSize:12.0f];
UIButton *button = [UIButton styledButtonWithBackgroundImage:image font:font title:title target:target selector:selector];
button.titleLabel.textColor = [UIColor whiteColor];
[button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
return [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
}
UIButton+StyledButton.h
#interface UIButton (UIButton_StyledButton)
+ (UIButton *)styledButtonWithBackgroundImage:(UIImage *)image font:(UIFont *)font title:(NSString *)title target:(id)target selector:(SEL)selector;
#end
UIButton+StyledButton.m
#implementation UIButton (UIButton_StyledButton)
+ (UIButton *)styledButtonWithBackgroundImage:(UIImage *)image font:(UIFont *)font title:(NSString *)title target:(id)target selector:(SEL)selector
{
CGSize textSize = [title sizeWithFont:font];
CGSize buttonSize = CGSizeMake(textSize.width + 20.0f, image.size.width);
UIButton *button = [[[UIButton alloc] initWithFrame:CGRectMake(0.0f, 0.0f, buttonSize.width, buttonSize.height)] autorelease];
[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button setTitle:title forState:UIControlStateNormal];
[button.titleLabel setFont:font];
return button;
}
#end
It's easy to use, e.g.:
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.leftBarButtonItem = [UIBarButtonItem styledBackBarButtonItemWithTarget:self selector:#selector(dismissModalViewController)];
self.navigationItem.rightBarButtonItem = [UIBarButtonItem styledSubmitBarButtonItemWithTitle:NSLocalizedString(#"Done", nil) target:self selector:#selector(doneButtonTouched:)];
}
The above code is from a project that's still work-in-progress, so it could be cleaned up a bit, but it works as supposed to. Use images without text as buttons and make sure they're stretchable (i.e. don't make the images too small and be careful with gradients). The image of the back button in the following example is only 31 x 30 pixels, but it's stretched to make the text fit.
Some examples of the results:
Back button
Cancel / Done buttons
I have used this code a few times:
- (void)viewDidLoad {
[super viewDidLoad];
// Set the custom back button
UIImage *buttonImage = [UIImage imageNamed:#"back_button.png"];
//create the button and assign the image
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
//set the frame of the button to the size of the image (see note below)
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(back) forControlEvents:UIControlEventTouchUpInside];
//create a UIBarButtonItem with the button as a custom view
UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.leftBarButtonItem = customBarItem;
// Cleanup
[customBarItem release];
}
-(void)back {
// Tell the controller to go back
[self.navigationController popViewControllerAnimated:YES];
}
From this website.
Hope it helps!
You have to build a custom UIButton and pass it to UIBarButton initWithCustomView.
Here is what you do.
1) Add custom buttons to your navigation item in the Interface Builder:
2) In the code do the following:
#define IS_IOS7 ([[[UIDevice currentDevice] systemVersion] compare:#"7.0" options:NSNumericSearch] != NSOrderedAscending)
- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];
if (IS_IOS7) {
[self shiftView:self.navigationItem.leftBarButtonItem.customView horizontallyBy:-11];
[self shiftView:self.navigationItem.rightBarButtonItem.customView horizontallyBy:11];
}
}
- (void)shiftView:(UIView*)view horizontallyBy:(int)offset {
CGRect frame = view.frame;
frame.origin.y += offset;
view.frame = frame;
}

How To Customize UINavBar?

I am using the following code to add a custom navbar to my app. But I want to be able to change the navbar depending on the view, is this possible?
#implementation UINavigationBar (UINavigationBarCategory)
- (void)drawRect:(CGRect)rect
{
UIColor *color = [UIColor colorWithRed:82/255.0 green:80/255.0 blue:81/255.0 alpha:1.0];
UIImage *img = [UIImage imageNamed: #"navbar.png"];
[img drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
self.tintColor = color;
}
#end
If not, is it possible to just add an image to the navbar instead of title text?
I would strongly discourage using a category to override -drawRect on a UINavBar as this design will break on a soon to be released iOS version.
It is very easy to create custom images and buttons to the navBar title though. Here's some code snippets I pulled directly out of production code for one of my apps. It creates a custom image button in the navBar's title area, but it's just as easy to create an image instead:
- (void) viewDidLoad {
UIButton *titleButton = [self buttonForTitleView];
[titleButton setTitle:newTitle forState:UIControlStateNormal];
[titleButton sizeToFit];
self.navigationBar.topItem.titleView = titleButton;
}
- (UIButton *) buttonForTitleView {
if (!_buttonForTitleView) {
UIImage *buttonImage = [UIImage imageNamed:#"button_collection_name2"];
UIImage *pressedButtonImage = [UIImage imageNamed:#"button_collection_name2_pressed"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: buttonImage forState : UIControlStateNormal];
[button setBackgroundImage: pressedButtonImage forState : UIControlStateHighlighted];
[button setFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)];
[button setTitleColor:[UIColor colorWithRed:38.0/255.0 green:38.0/255.0 blue:38.0/255.0 alpha:1.0] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0] forState:UIControlStateNormal];
button.titleLabel.adjustsFontSizeToFitWidth = YES;
button.titleLabel.minimumFontSize = 10.0;
button.titleEdgeInsets = UIEdgeInsetsMake(0.0, 10.0, 0.0, 10.0);
button.titleLabel.shadowOffset = (CGSize){ 0, 1 };
[button addTarget:self action:#selector(presentWidgetCollectionSwitchingViewController:) forControlEvents:UIControlEventTouchUpInside];
_buttonForTitleView = [button retain];
}
return _buttonForTitleView;
}
And this is what the button looks like in the app VideoBot:
Here's an example of using an image for the navbar title:
UIImage *titleImage = [UIImage imageNamed:#"navbar_title_image"];
UIImageView *titleImageView = [[[UIImageView alloc] initWithImage:titleImage] autorelease];
UIView *container = [[[UIView alloc] initWithFrame:(CGRect){0.0, 0.0, titleImage.size.width, titleImage.size.height}] autorelease];
container.backgroundColor = [UIColor clearColor];
[container addSubview:titleImageView];
// add the view to the title
self.navigationBar.topItem.titleView= container;

Custom UIButton for Iphone

I have an view in my App which has a number of buttons based on the number of items returned by the server. So if the server returns say 10 items, there should be 10 buttons and clicking on each button should call a different person.
For the above purpose I created a custom button class deriving from UIButton.
#implementation HopitalButton
#synthesize index;
#synthesize button_type;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
UIImage* img = [UIImage imageNamed:#"dr_btn.png"];
[img stretchableImageWithLeftCapWidth:10 topCapHeight:10];
[self setBackgroundImage:img forState:UIControlStateNormal];
[self setTitleColor:[UIColor colorWithRed:0.698 green:0.118 blue:0.376 alpha:1] forState:UIControlStateNormal] ;
[self setFont:[UIFont fontWithName:#"Helvetica Bold" size:13]];
self.titleLabel.textColor = [UIColor colorWithRed:178 green:48 blue:95 alpha:1];
self.adjustsImageWhenHighlighted = YES;
}
return self;
}
- (void)dealloc {
[super dealloc];
}
#end
Now the problem with the above code is that it does not create buttons that look similar to the buttons created by default in Interface builder. The borders are missing.
And I create buttons of the above type by the following code:
HopitalButton* hb = [[HopitalButton alloc] init];
hb.button_type = #"call";
hb.frame = CGRectMake(50, 50 + i * 67, 220, 40);
[self.scroll_view addSubview:hb];
[hb setTitle:[[[self.office_full_list objectAtIndex:i] objectForKey:#"Staff" ]objectForKey:#"FullName"] forState:UIControlStateNormal];
hb.index = [NSNumber numberWithInt:[self.button_items count]];
[self.button_items insertObject:hb atIndex:[self.button_items count]];
[hb addTarget:self action:#selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
I am not finding a way to set the button type for this custom button.
Is there a way i can do it ? Or is there a better way to design the code.
You first start with a stretchable image with a border:
alt text http://grab.by/4lP
Then you make a button with the stretched image as the background and apply text.
INDEX_OFFSET = 82753; // random
UIButton *sampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
[sampleButton setFrame:CGRectMake(kLeftMargin, 10, self.view.bounds.size.width - kLeftMargin - kRightMargin, 52)];
[sampleButton setTitle:#"Button Title" forState:UIControlStateNormal];
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
[sampleButton setTag:<INDEX>+INDEX_OFFSET];
[sampleButton setBackgroundImage:[[UIImage imageNamed:#"redButton.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[sampleButton addTarget:self action:#selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:sampleButton];
Obviously, you will need to adjust the frame origin and size to match your app, as well as the target, selector, and title. And
[sampleButton setFont:[UIFont boldSystemFontOfSize:20]];
setFont is now deprecated, use titleLabel.font property instead
sampleButton.titleLabel.font = [UIFont boldSystemFontOfSize:20];
you can use individual class for custom Roundrect button which can be useful in whole project with your specific frame style as below
#import <UIKit/UIKit.h>
#import <QuartzCore/QuartzCore.h>
#interface CustomRoundRectButton : UIButton
#end
#import "CustomRoundRectButton.h"
#implementation CustomRoundRectButton
- (void)drawRect:(CGRect)rect
{
[[self layer] setMasksToBounds:YES];
[self.layer setCornerRadius:10.0f];
[self.layer setBorderColor:[UIColor grayColor].CGColor];
[self.layer setBorderWidth:1.0];
}
#end
In this you have to select button type custom and select its class as CustomRoundRectButton.
For Simple custom button we can use as below
-(UIBarButtonItem*)BackButton
{
UIButton*btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
[btn setFrame:CGRectMake(0, 0, 30, 30)];
[btn addTarget:self action:#selector(actionBack) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem*barBtn = [[[UIBarButtonItem alloc] initWithCustomView:btn] autorelease];
return barBtn;
}
Shouldn't you be calling initWithFrame: rect instead of:
HopitalButton* hb = [[HopitalButton alloc] init];