码迷,mamicode.com
首页 > 其他好文 > 详细

xcode UIButton创建、监听按钮点击、自定义按钮 、状态 、内边距

时间:2016-06-06 21:52:36      阅读:290      评论:0      收藏:0      [点我收藏+]

标签:

代码创建

//创建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#>);

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

xcode UIButton创建、监听按钮点击、自定义按钮 、状态 、内边距

标签:

原文地址:http://www.cnblogs.com/fleas/p/5565117.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!