您的位置:首页 > 产品设计 > UI/UE

iOS开发 ----- UIButton

2015-09-02 20:21 597 查看

UIButton

//创建 button 也可以用alloc init方式创建 button的类型为自定义类型
UIButton * button2 = [UIButton buttonWithType:UIButtonTypeCustom];
//初始化两张图片
UIImage * image = [UIImage imageNamed:@"1.png"];
UIImage * bgImage = [UIImage imageNamed:@"map.png"];
//设置button的前景图
[button2 setImage:image forState:UIControlStateNormal];
//设置button的背景图
[button2 setBackgroundImage:bgImage forState:UIControlStateNormal];
//设置button的位置大小
button2.frame = CGRectMake(0, 80, 375, 30);
//设置点击的时候是否可以高亮
button2.showsTouchWhenHighlighted = YES;
//添加到视图上
[self.view addSubview:button2];

//初始化button
UIButton * button3 = [UIButton buttonWithType:UIButtonTypeCustom];
button3.frame = CGRectMake(0, 120, 375, 30);
//设置button的标题
[button3 setTitle:@"按钮" forState:UIControlStateNormal];
//设置高亮时的标题
[button3 setTitle:@"llalala" forState:UIControlStateHighlighted];
//设置背景颜色
[button3 setBackgroundColor:[UIColor grayColor]];
//设置button的字体的颜色
[button3 setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
//设置button是否可用
button3.enabled = YES;
//设置button的唯一标示符
button3.tag = 101;
//button的点击方法 自己写onClick方法即可
[button3 addTarget:self action:@selector(onClick:)forControlEvents:UIControlEventTouchUpInside];

//其实button上边的字体相当于一个label,可以利用label的相关属性进行设置

//设置圆角矩形
button3.layer.cornerRadius = 10;
//设置边框的颜色
button3.layer.borderColor = [UIColor grayColor].CGColor;
//设置边框的宽度
button3.layer.borderWidth = 3;
//设置button上的字体的大小
button3.titleLabel.font = [UIFont fontWithName:@"Menlo" size:30];
//设置字体的背景颜色
button3.titleLabel.backgroundColor = [UIColor redColor];
//设置 图片的位置 上左下右
button.imageEdgeInsets = UIEdgeInsetsMake(10, 20, 10, 40);
//设置 内容的位置 上左下右
button.contentEdgeInsets = UIEdgeInsetsMake(30, 10, 0, 0);
//设置 文字的位置 上左下右
button.titleEdgeInsetsEdgeInsets = UIEdgeInsetsMake(30, 10, 0, 0);
//把button添加到view上
[self.view addSubview:button3];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: