1.说明【UIButton】
UIButton是一个视图类,继承自UIControl 间接继承自UIView,凡是UIControl子类可以接收触发事件。
UIButton用于创建一个按钮,添加点击事件
2.常见的三种状态
UIControlStateNormal 正常状态
UIControlStateHighlighted 高亮状态
UIControlStateSelected 选中状态(设置btn.selected)
//iOS9.0以后❤️加了一个特性,
uiControlStateFocused//聚焦状态(应该和3d touch有关)
3.常用方法
//设置title
- (void)setTitle:(NSString *)title forState:(UIControlState)state;
//设置title的颜色
- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
//设置前景图 用的是填充方式的第二种
- (void)setImage:(UIImage *)image forState:(UIControlState)state;
//设置背景图片
- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;
//给button添加方法
- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle; // 当前标题
@property(nonatomic,readonly,strong) UIColor *currentTitleColor; // 当前标题颜色
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor; // 当前标题阴影颜色
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage; // 当前图片
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage; // 当前背景图片
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // 当前富文本标题
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); // 当前标题UILabel
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0); // 当前图片UIImageView
4.调整间距
btn.contentEdgeinsets = uiedgeinsetsmake();//按钮整体内容四周间距
btn.titleedgeinsets=同上//标题四周间距
//图片四周间距
btn.imageedgeinsets=tongshang
5.点击事件
-(void)btnClack:(UIButton *)slend{
//多个按钮,选中其中一个其他切换成未选中状态
//思路:先遍历该视图中有所少个btn,然后判断是不是btn
for (UIButton *btn in self.view.subviews) {
if ([btn isKindOfClass:[UIButton class]]) {
//未选中按钮设置
btn.selected = NO;
}
}
// //这边写选中按钮设置
}
6新增属性
//button如果设置了图片的情况下 还有一个酷炫的点击效果
//btn.showsTouchWhenHighlighted = YES;