iOS UIButton常用属性大全

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;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值