- //这里创建自定义的按钮
- UIButton *buttonExample = [UIButton buttonWithType:UIButtonTypeCustom];
- // button类型有以下6种,
- // typedef enum {
- // UIButtonTypeCustom, 自定义风格
- // UIButtonTypeRoundedRect, 圆角矩形
- // UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用
- // UIButtonTypeInfoLight, 亮色感叹号
- // UIButtonTypeInfoDark, 暗色感叹号
- // UIButtonTypeContactAdd, 十字加号按钮
- // } UIButtonType;
- //位置
- buttonExample.frame = CGRectMake(0, 100, 100, 40);
- //背景色
- buttonExample.backgroundColor = [UIColor clearColor];
- //填充图片
- [buttonExample setImage:[UIImage imageNamed:@"pig.png"] forState:UIControlStateNormal];
- //背景图片
- [buttonExample setBackgroundImage:[UIImage imageNamed:@"pig.png"] forState:UIControlStateNormal];
- //标题
- [buttonExample setTitle:@"点击" forState:UIControlStateNormal];
- //forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现
- // enum {
- // UIControlStateNormal 常规状态显现
- // UIControlStateHighlighted 高亮状态显现
- // UIControlStateDisabled 禁用的状态才会显现
- // UIControlStateSelected 选中状态
- // UIControlStateApplication, 当应用程序标志时
- // UIControlStateReserved 为内部框架预留,可以不管他
- // };
- //禁用高亮状态
- buttonExample.adjustsImageWhenHighlighted = NO;
-
- //高亮状态
- buttonExample.showsTouchWhenHighlighted = YES;
-
- //圆角
- headLogoButton.layer.masksToBounds = YES;
- headLogoButton.layer.cornerRadius = headLogoButton.frame.size.width/2;
-
- //添加事件 action:方法 forControlEvents:什么时候执行
- [buttonExample addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
-
- //添加
- [self.view addSubview:buttonExample];
UIButton常用属性介绍lzp
最新推荐文章于 2023-05-16 11:00:00 发布