·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> app软件开发 >> IOS开发 >> ios开发中button控件的属性及常见问题

ios开发中button控件的属性及常见问题

作者:佚名      IOS开发编辑:admin      更新时间:2022-07-23

   最为最基本的控件,我们必须对button的每个常用属性都熟练应用;

1,使用之前,必须对按钮进行定义,为乐规范,在@interface ViewController (){}中进行定义,先定义后使用。

    UIButton *_button1;         在实际的项目开发中,变量名尽可能长一点,并且在变量名前加"_" 下划线字符(规范)

2,接下来在@implementation ViewController中对按钮的属性进行赋值

    _button1=[UIButton buttonWithType:UIButtonTypeRoundedRect];     //设置按钮的样式

  

       UIButtonTypeCustom = 0, 自定义风格

   UIButtonTypeRoundedRect, 圆角矩形

   UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用  

  UIButtonTypeInfoLight, 亮色感叹号

  UIButtonTypeInfoDark, 暗色感叹号

  UIButtonTypeContactAdd, 十字加号按钮

 

3,为按钮设置背景颜色

  _button1.backgroundColor=[UIColor whiteColor];

4,设置按钮在图中的显示位置和大小

_button1.frame=CGRectMake(100, 100, 40, 40);

5,给按钮起名字和名字的颜色

[_button1 setTitle:@"点击" forState:UIControlStateNormal];       (点击是按钮的名字)

[_button setTitleColor:[UIColorredColor]forState:UIControlStateNormal];

6,按钮也有好多状态

   forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显

现在只会用UIControlStateNormal 其他的暂时用不到

7,按钮上可以放图片

[_button1 setImage:[UIImageimageNamed:@"11.png"]forState:UIControlStateNormal];

[_button1 setBackgroundImage:[UIImageimageNamed:@"22.png"]forState:UIControlStateNormal];

8,关于按钮显示时,总显示是方形的问题

[startBtn.layer setCornerRadius:8];//设置按钮圆弧的角度

加入这句可以使按钮变成圆角

9,点击按钮,使按钮有所反应(添加事件)

[_button1 addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside]; (start是函数名)

 10,按钮在普通状态和高亮状态颜色的设置

[_button1 setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted]; //在按钮被按下去的,按钮的整个会变成黄色

[_button1 setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];//按钮在普通状态下,将按钮上的标题文字设置为蓝色