//创建UIButton

        UIButton * btnType=[[UIButton alloc]init];

        //设置UIControlStateNormal状态下的文字颜色

        [btnType setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

        //设置字体大小

        btnType.titleLabel.font=[UIFont systemFontOfSize:9.0];

        //设置边框的宽度

        btnType.layer.borderWidth=1;

        //设置边框的颜色

        btnType.layer.borderColor=[[UIColor lightGrayColor]CGColor];

        //设置UIControlStateNormal的文字

        [btnType setTitle:@"按钮设置名字" forState:UIControlStateNormal];

        //设置UIControlStateNormal的图片

        [btnType setImage:[UIImage imageNamed:@"获取Assets.xcassets图片名称"] forState:UIControlStateNormal];

        //设置UIControlStateNormal背景图片

        [btnType setBackgroundImage:[UIImage imageNamed:@"获取Assets.xcassets图片名称"] forState:UIControlStateNormal];

        //设置失效状态

        btnType.enabled=NO;

监听按钮点击

//只要按钮触发了UIControlEventTouchUpInside事件,就调用self对象buttonClick方法

[btn addTarget:self action:@selector(buttonClick) forCOntrolEvents:UIControlEventTouchUpInside];

 

自定义按钮

-(CGRect)titleRectForContentRect:(CGRect)contentRect{

        // 返回文字的frame   

 } 

-  (CGRect)imageRectForContentRect:(CGRect)contentRect{

        // 返回图片的frame

 }

 

按钮显示的状态

设置不能点击 enabled 等于NO

adjustsImageWhenDisabled 等于NO  在Disabled下要不要调整显示的图片

adjustsImageWhenHighlighted 高亮下不要调整图片

 

按钮内边距

通过代码设置   contentEdgeINsets =   uiedgeInsetsMake 设置内边距

 contentEdgeInsets=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);

设置文字  titleEdgeInsets=uiedgeInsetsMake 设置文字的内边距

titleEdgeInsets=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);

设置图片 imageEdgeInsets=uiedgeInsetsMake 设置图片的内边距

imageEdgeInsetss=UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>);

微小调整使用内边距     复杂的话使用自定义按钮

相关文章:

  • 2021-10-29
  • 2021-08-30
  • 2021-08-08
  • 2018-02-23
  • 2021-11-17
  • 2021-12-19
  • 2021-06-19
  • 2021-10-19
猜你喜欢
  • 2021-12-26
  • 2021-02-27
  • 2021-09-30
  • 2021-12-26
  • 2021-07-07
  • 2021-11-01
  • 2021-10-19
  • 2021-12-27
相关资源
相似解决方案