Skip to content

AlexHsieh/ButtonAppearance

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ButtonAppearance

CI Status Version License Platform

Usage

If you need different border color or shadow color on different control state, subclass AHButton is required.

Shadow changed with state

[self.buttonFromStoryBoard setShadowColor:shadowColor  offset:CGSizeMake(0, 3.0) opacity:1.0 radius:1.0 forState:UIControlStateNormal];
[self.buttonFromStoryBoard setShadowColor:buttonColor  offset:CGSizeMake(0, 3.0) opacity:1.0 radius:1.0 forState:UIControlStateHighlighted];

Border changed with state

[self.buttonFromStoryBoard2 setCornerRadius:10.0 borderWidth:1.0 borderColor:[UIColor blackColor] forState:UIControlStateNormal];
[self.buttonFromStoryBoard2 setCornerRadius:10.0 borderWidth:1.0 borderColor:[UIColor grayColor] forState:UIControlStateHighlighted];

===========================

If you only need to change UIButton background color or background gradient on different control state, use category UIbutton+Appearance.

Background color changed with state

[button3 setBackgroundColor:[UIColor yellowColor] forState:UIControlStateNormal];
[button3 setBackgroundColor:[UIColor greenColor] forState:UIControlStateHighlighted];

Background gradient changed with state

[button4 setBackgroundGradient:@[[UIColor grayColor],[UIColor blackColor]] forState:UIControlStateNormal];
[button4 setBackgroundGradient:@[[UIColor lightGrayColor],[UIColor blackColor]] forState:UIControlStateHighlighted];

Border and shadow changed with state:

AHButton *button5 = [[AHButton alloc] initWithFrame:CGRectMake(0, 0, 280, 45)];
[button5 setShadowColor:normalShadowColor offset:CGSizeMake(0, 3.0) opacity:1.0 radius:0 forState:UIControlStateNormal];
[button5 setShadowColor:highlightShadowColor offset:CGSizeMake(0, 3.0) opacity:1.0 radius:0 forState:UIControlStateHighlighted];
[button5 setCornerRadius:5.0 borderWidth:1.0 borderColor:[UIColor blackColor] forState:UIControlStateNormal];
[button5 setCornerRadius:10.0 borderWidth:1.0 borderColor:[UIColor brownColor] forState:UIControlStateHighlighted];

===========================

There are couple hint here if you can't find it work well.

  1. Use UIButtonTypeCustom
  2. If you use autolayout, archieved button size may not be the same as you see on the nib. It could be this reason to make button background not right. Try log it to see if the size is what you expected.

Requirements

ARC only; iOS 8.0+

Installation

via Cocoa pods ButtonAppearance is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "ButtonAppearance"

Author

Alex Hsieh, [email protected]

License

ButtonAppearance is available under the MIT license. See the LICENSE file for more info.