UIButton的基本使用
来源:互联网 发布:c语言n次方 编辑:程序博客网 时间:2024/05/22 15:09
在iOS中按钮的使用频率是非常高的,也是用户与界面之间交互的重要元素,下面将详细讲解UIButton的详细使用和在开发中经常需要使用的情况。
创建
/*按钮类型UIButtonTypeCustom = 0,UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),UIButtonTypeDetailDisclosure,UIButtonTypeInfoLight,UIButtonTypeInfoDark,UIButtonTypeContactAdd,UIButtonTypeRoundedRect = UIButtonTypeSystem,*/UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];一般创建的按钮类型为
UIButtonTypeCustom
如果使用
UIButtonTypeSystem有时会有问题存在。其它类型一般很少使用
设置按钮的大小和背景颜色
button.backgroundColor = [UIColor blackColor];button.frame = CGRectMake(20, 100, 64, 44);在iOS中按钮的大小设置一般为宽64,高44,这样正好是半个手指的大小。
UIButton的属性
@property(nonatomic) UIEdgeInsets contentEdgeInsets //内容的属性可以上上下左右有一块不显示<span style="color:#FF0000;">//下面两个属性经常被使用</span>
@property(nonatomic) UIEdgeInsets titleEdgeInsets //设置按钮上面文字的位置调整
@property(nonatomic) UIEdgeInsets imageEdgeInsets //设置按钮上面图片的位置调整@property(nonatomic) BOOL adjustsImageWhenHighlighted //当按钮被按下时图片高亮显示@property(nonatomic) BOOL adjustsImageWhenDisabled //当按钮被按下时图片图片变灰不可用@property(nonatomic) BOOL showsTouchWhenHighlighted //当按钮被按下时高亮显示
注意:按钮有两种状态属性经常使用1.普通状态 2.按下状态(高亮状态)
按钮的状态枚举,以下为常用,还有三种不常使用 ps:(很多情况下按钮的多种状态是通过自定义按钮,然后对按钮状态进行枚举创建的)
UIControlStateNormal //普通状态
UIControlStateHighlighted //高亮状态,按钮后的状态
UIControlStateDisabled //不可选中状态
UIControlStateSelected //按钮选中状态
设置按钮里面内容
//设置文字[button setTitle:@"12345" forState:UIControlStateNormal];//设置图片[button setImage:[UIImage imageNamed:@"图片名"] forState:UIControlStateNormal];
按钮的属性状态
@property(nonatomic,getter=isEnabled) BOOL enabled;//是否为可用状态@property(nonatomic,getter=isSelected) BOOL selected; //是否为选中状态@property(nonatomic,getter=isHighlighted) BOOL highlighted; //是否为高亮状态@property(nonatomic) UIControlContentVerticalAlignment contentVerticalAlignment; //内容垂直的位置样式中心,顶部,底部,全部@property(nonatomic) UIControlContentHorizontalAlignment contentHorizontalAlignment; //内容水平的位置样式中心,底部,全部@property(nonatomic,readonly) UIControlState state; //按钮的状态
还有两个不常用的属性
@property(nonatomic,readonly,getter=isTracking) BOOL tracking;@property(nonatomic,readonly,getter=isTouchInside) BOOL touchInside;
监听方法
- (BOOL)beginTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;- (BOOL)continueTrackingWithTouch:(UITouch *)touch withEvent:(nullable UIEvent *)event;- (void)endTrackingWithTouch:(nullable UITouch *)touch withEvent:(nullable UIEvent *)event;- (void)cancelTrackingWithEvent:(nullable UIEvent *)event;
设置按钮内容和状态
//设置文字- (void)setTitle:(nullable NSString *)title forState:(UIControlState)state;//设置文字颜色- (void)setTitleColor:(nullable UIColor *)color forState:(UIControlState)state;//设置阴影- (void)setTitleShadowColor:(nullable UIColor *)color forState:(UIControlState)state;//设置图片- (void)setImage:(nullable UIImage *)image forState:(UIControlState)state;//设置背景图片- (void)setBackgroundImage:(nullable UIImage *)image forState:(UIControlState)state;点击事件的几种触发方式ps:只写常用的几个类
UIControlEventTouchDown = 0 //按下UIControlEventTouchDownRepeat = 1 //多次点击<span style="color:#FF0000;">UIControlEventTouchUpInside = 6 //点击(一般为这个)</span>UIControlEventTouchUpOutside = 7 //点击拿起后UIControlEventTouchCancel = 8 //点击取消添加点击事件
- (void)addTarget:(nullable id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;e.g./*addTarget: 添加给谁,一般都为selfaction: 执行的方法*/[button addTarget:self action:@selector(buttonClike:) forControlEvents:UIControlEventTouchUpInside];//移除点击事件- (void)removeTarget:(nullable id)target action:(nullable SEL)action forControlEvents:(UIControlEvents)controlEvents;
按钮的高级使用
A:按钮可以同时添加图片和文字,但是默认的是图片在左,文字在右,但在开发中一般要求文字在左,图片在右,这样就需要调节
@property(nonatomic) UIEdgeInsets titleEdgeInsets //设置按钮上面文字的位置调整
@property(nonatomic) UIEdgeInsets imageEdgeInsets //设置按钮上面图片的位置调整这两个属性来改变图片和文字的位置。
B:按钮有时候要设置里面文字的样式
//按钮字体大小button.titleLabel.font = [UIFont systemFontOfSize:17];//按钮字体颜色[button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];C: 按钮倒圆角和边框样式
//边框宽度button.layer.borderWidth = 2;//边框颜色button.layer.borderColor = [UIColor redColor].CGColor;
D:多个按钮的区分一般使用Tag
在点击事件中来通过tag值来判断是哪个按钮的点击事件
ps:如有写的不对的地方和需要完善的地方请留言或发送邮件到2450862074@qq.com
2 0
- UIButton的基本使用
- 基本控件UIButton的使用
- 1.UIButton的基本使用
- IOS的基本控件的使用-UIButton
- UIButton的基本用法
- UIButton的基本属性
- UIButton的基本用法
- IOS的基本控件的使用-UIButton的事件处理
- iOS开发UI系列之UIButton的基本使用
- UIButton的一些基本用法
- UIButton的使用
- UIButton的使用
- UIButton 使用的感受
- UIButton的使用
- UIButton的简单使用
- swift UIButton的使用
- swift UIButton的使用
- UIButton的使用细节
- 用几句话实现意想不到的功能2(被忽略的TextView属性)
- Ubuntu下Nginx简单使用
- javascript 没有块级作用域
- C#中A a=new B()的意义
- Axis2学习总结(3)在客户端配置log4j监控soap消息
- UIButton的基本使用
- 多个Form提交同一地址技巧(form序列化提交 java反序列化表单)
- 世界黑客编程大赛第一名的作品
- Android:利用SharedPreferences实现自动登录
- RAD Studio, Delphi, C++Builder 10.1 Berlin第二季优惠活动
- 压力 性能 测试 工具 ab (apache benchmark) jmeter
- 推荐几款实用的Android Studio 插件
- 最大回文子串(java)
- Android View的测量