Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Styling Buttons in IOS Xcode

I am new to IOS development. In windows phone development we use to define a style(appearance e.g) of the button control in app Global place and just use that style anywhere on any button in the app by assigning the style property.

Is there anything similar in IOS XCode? I know I can customize the button in the property and appearance windows in XCode. But in this way I have to give style to each and every similar button in the app. and if there is any change I have to change it everywhere. What IOS developers do in these kind situations?

like image 411
Muhammad Saifullah Avatar asked Sep 01 '15 16:09

Muhammad Saifullah


3 Answers

If you want to style all buttons in your app in a standard way, then you would style the appearance proxy for UIButton. You get one by calling the class method UIButton.appearance(), and then styling the returned value the way you want your buttons to look. You can also do a more fine-grained appearance by calling methods like UIButton.appearanceWhenContainedIn().

like image 116
NRitH Avatar answered Nov 19 '22 12:11

NRitH


Create a UIButton subclass. For example for creating a UIButton with rounded corners and a border:

Header file

#import <Foundation/Foundation.h>

@interface XYRoundedCornerButton : UIButton
@end

Implementation file

...import headers etc...
@implementation XYRoundedCornerButton

- (void)awakeFromNib {
    [super awakeFromNib];
    self.layer.cornerRadius = 8.0f;
    self.layer.masksToBounds = YES;
    self.layer.borderRadius = 1.0f;
    self.layer.borderColor = UIColor.redColor;
}

@end
like image 3
MarkHim Avatar answered Nov 19 '22 11:11

MarkHim


If you are using storyboard, You don't need to add any code in class. Just drag one button and set all the properties you want, whether background colour, corner radius etc, whatever you want. And then either copy it and paste wherever you want or select the button, ALT+DRAG it .

like image 2
simardeep singh Avatar answered Nov 19 '22 13:11

simardeep singh