Create a UIButton in iPhone Objective C

To create a UIButton in iPhone, the following code gives the standard features that needs to be initialized.


Two images are used, one for Normal state and other for Highlighted state.


UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setButtonType:UIButtonTypeCustom];
button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;

[button setTitle:title forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];

UIImage *newImage = [image stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0];
[button setBackgroundImage:newImage forState:UIControlStateNormal];

UIImage *newPressedImage = [imagePressed stretchableImageWithLeftCapWidth:12.0
topCapHeight:0.0];
[button setBackgroundImage:newPressedImage forState:UIControlStateHighlighted];

[button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];

// in case the parent view draws with a custom color or gradient, use a transparent color
button.backgroundColor = [UIColor clearColor];

Comments

Popular posts from this blog

Integrating ZXing QR Code reader in iPhone / iOS applications

Multiple line of text in UIPickerView

Custom Message extension using Messages SDK - iMessage App store