UI笔记_UIButton

来源:互联网 发布:阿里云备案邮寄资料 编辑:程序博客网 时间:2024/04/29 04:39

UIButton

// 1、创建button对象(如果本类有初始化⽅方法,使⽤用⾃自⼰己的;否则使⽤用 ⽗父类的)。
UIButton *button = [[UIButton buttonWithType:UIButtonTypeSystem];// 2、设置按钮显⽰示相关的属性
[button setFrame:CGRectMake(20, 120, 280, 50)];[button setTitle:@"button" forState:UIControlStateNormal];// 3、为按钮添加点击事件
 [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];// 4、添加按钮到⽗父视图上,⽤用以显⽰示
 [self.window addSubview:button];// 5、按钮⽆无需释放(因为使⽤用的是类⽅方法创建的button)
UIButton添加事件

addTarget:action:forControlEvents:           为按钮添加事件,指定按钮点击之后,执行target的action方法

removeTarget:action:forControlEvents:     移除按钮的点击事件

外观控制

setImage:forState:                       设置指定状态下的前景图片

imageForState:                            获取指定状态下的前景图片

setBackgroundImage:forState:    设置指定状态下的背景图片

backgroundImageForState:         获取指定状态下的背景图片

圆角矩形的按钮

对按钮的layer属性中的边框宽度圆角弧度设置即可获得圆角矩形的按钮

[button.layer setBorderWidth:1];       设置按钮边框宽度

[button.layer setCornerRadius:5];     设置按钮边框角的圆角弧度

0 0
原创粉丝点击