经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 移动开发 » iOS » 查看文章
ios开发UI篇--UIButton
来源:cnblogs  作者:久依  时间:2019/3/7 8:46:40  对本文有异议

概述

  • UIButton 是执行自定义代码以响应用户交互的控件。
  • UIButton 其实包含 UIImageViewUILabel 两个控件,UIButton 继承于 UIControl,所以有 addtarget 监听事件

属性和方法

初始化Button不用alloc init方法,使用便利构造器方法

  1. UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

UIButton的类型如下

UIButton类型说明
UIButtonTypeCustom 没有按钮样式,一般设置该样式,根据需要自定义
UIButtonTypeSystem 系统样式按钮,例如导航栏和工具栏中显示的按钮。
UIButtonTypeDetailDisclosure 详细披露按钮。
UIButtonTypeInfoLight 具有浅色背景的信息按钮。
UIButtonTypeInfoDark 信息按钮有一个黑暗的背景。
UIButtonTypeContactAdd 联系人添加按钮。
UIButtonTypePlain 没有模糊背景视图的标准系统按钮。
UIButtonTypeRoundedRect 已经废弃,使用UIButtonTypeSystem代替

UIButton的状态如下

UIButton的状态说明
UIControlStateNormal 控件的正常状态或默认状态 - 即已启用但未选中或高亮显示。
UIControlStateHighlighted 突出显示的控制状态。按钮处于选中状态时的状态
UIControlStateDisabled 一个控件的禁用状态。
UIControlStateSelected 选择一个控件的状态。
UIControlStateFocused 集中控制状态。
UIControlStateApplication 额外的控制状态标志可用于应用程序使用。
UIControlStateReserved 控制状态标志保留给内部框架使用。

设置frame

  1. [btn setFrame:CGRectMake(100, 100, 100, 30)];

设置某一状态下的标题

  1. [btn setTitle:@"测试" forState:(UIControlStateNormal)];

设置某一状态下的标题颜色

  1. [btn setTitleColor:[UIColor redColor] forState:(UIControlStateNormal)];

设置某一状态下的阴影颜色

  1. [btn setTitleShadowColor:[UIColor purpleColor] forState:UIControlStateNormal];

设置某一状态下的背景颜色

  1. [btn setBackgroundColor:[UIColor blackColor]];

设置标题字体大小

  1. btn.titleLabel.font = [UIFont systemFontOfSize:30];

设置某一状态下的背景图片(背景图片显示在其标题和前景图像后面。)

  1. [btn setBackgroundImage:[UIImage imageNamed:@"登录logo"] forState:(UIControlStateNormal)];

设置某一状态下的前景图片

  1. [btn setImage:[UIImage imageNamed:@"验证码"] forState:(UIControlStateNormal)];

设置标题内边距

  1. btn.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0); top, left, bottom, right;

 

设置图片内边距

  1. btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);

设置内容内边距

  1. btn.contentEdgeInsets = UIEdgeInsetsMake(10, -30, 10, 10);

标题的阴影改变时,按钮是否高亮显示。默认为NO

  1. btn.reversesTitleShadowWhenHighlighted = YES;

按钮高亮的情况下,图像的颜色是否要加深一点。默认是YES

  1. btn.adjustsImageWhenHighlighted = YES;

按钮禁用的情况下,图像的颜色是否要加深一点。默认是YES

  1. btn.adjustsImageWhenDisabled = YES;

按下按钮是否会发光 默认是NO

  1. btn.showsTouchWhenHighlighted = NO;

返回button 某个状态下的标题

  1. - (nullable NSString *)titleForState:(UIControlState)state;

返回button 某个状态下的标题颜色

  1. - (nullable UIColor *)titleColorForState:(UIControlState)state;

返回button 某个状态下的阴影标题颜色

  1. - (nullable UIColor *)titleShadowColorForState:(UIControlState)state;

返回button 某个状态下的图片

  1. - (nullable UIImage *)imageForState:(UIControlState)state;

返回button 某个状态下的背景图片

  1. - (nullable UIImage *)backgroundImageForState:(UIControlState)state;

返回button 某个状态下的富文本标题

  1. - (nullable NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);

获取按钮当前标题

  1. NSString *title = btn.currentTitle;

获取按钮当前标题颜色

  1. UIColor *color = btn.currentTitleColor;

获取按钮当前阴影标题颜色

  1. UIColor *shandowColor = btn.currentTitleShadowColor;

获取按钮当前按钮内图像

  1. UIImage *image = btn.currentImage;

获取按钮当前标题背景图片

  1. UIImage *backgroundImage = btn.currentBackgroundImage;

获取按钮当前标题富文本

  1. NSAttributedString *aString = btn.currentAttributedTitle;

指定背景边界

  1. - (CGRect)backgroundRectForBounds:(CGRect)bounds;

指定内容边界

  1. - (CGRect)contentRectForBounds:(CGRect)bounds;

指定标题边界

  1. - (CGRect)titleRectForContentRect:(CGRect)contentRect;

指定图片边界

  1. - (CGRect)imageRectForContentRect:(CGRect)contentRect;

给按钮添加点击事件

  1. [btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];

UIButton点击事件如下

UIButton点击事件说明
UIControlEventTouchDown 单点触摸按下事件:用户点触屏幕,或者又有新手指落下的时候。
UIControlEventTouchDownRepeat 多点触摸按下事件,点触计数大于1:用户按下第二、三、或第四根手指的时候。
UIControlEventTouchDragInside 当一次触摸在控件窗口内拖动时。
UIControlEventTouchDragOutside 当一次触摸在控件窗口之外拖动时。
UIControlEventTouchDragEnter 当一次触摸从控件窗口之外拖动到内部时
UIControlEventTouchDragExit 当一次触摸从控件窗口内部拖动到外部时。
UIControlEventTouchUpInside 所有在控件之内触摸抬起事件
UIControlEventTouchUpOutside 所有在控件之外触摸抬起事件(点触必须开始与控件内部才会发送通知)。
UIControlEventTouchCancel 所有触摸取消事件,即一次触摸因为放上了太多手指而被取消,或者被上锁或者电话呼叫打断。
UIControlEventValueChanged 当控件的值发生改变时,发送通知。用于滑块、分段控件、以及其他取值的控件。你可以配置滑块控件何时发送通知,在滑块被放下时发送,或者在被拖动时发送。
UIControlEventEditingDidBegin 当文本控件中开始编辑时发送通知
UIControlEventEditingChanged 当文本控件中的文本被改变时发送通知。
UIControlEventEditingDidEnd 当文本控件中编辑结束时发送通知。
UIControlEventEditingDidEndOnExit 当文本控件内通过按下回车键(或等价行为)结束编辑时,发送通知。
UIControlEventAllTouchEvents 通知所有触摸事件
UIControlEventAllEditingEvents 通知所有关于文本编辑的事件。
UIControlEventApplicationReserved range available for application use
UIControlEventSystemReserved range reserved for internal framework use
UIControlEventAllEvents 通知所有事件

 


作者:coder小鹏

 

原文链接:http://www.cnblogs.com/jiuyi/p/10485365.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号