/*
背景风格
UIControlStateNormal = 0, 常规显示
UIControlStateHighlighted = 1 << 0, 被点击按住之后显示的状态
UIControlStateDisabled = 1 << 1, 当按钮被禁用得时候才显示
UIControlStateSelected = 1 << 2, 当按钮被选择的时候才显示
UIControlStateApplication = 0x00FF0000, 当应用程序标时
UIControlStateReserved = 0xFF000000 内部框架预留
*/
/*
按钮风格
UIButtonTypeCustom = 0, 自定义风格
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), 默认风格
UIButtonTypeDetailDisclosure, 小箭头蓝色圆圈按钮
UIButtonTypeInfoLight, 亮蓝色圆圈按钮
UIButtonTypeInfoDark, 暗蓝色圆圈按钮
UIButtonTypeContactAdd, 加号按钮
UIButtonTypeRoundedRect = UIButtonTypeSystem, 默认风格
*/
[super viewDidLoad];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
[button setImage:[UIImage imageNamed:@"chiniupa"] forState:UIControlStateSelected];
//设置按钮被禁用
[button setEnabled:FALSE];
//当按钮被选择的
[button setSelected:TRUE];
//设置标题按钮
[button setTitle:@"hello" forState:UIControlStateHighlighted];
//设置背景颜色
[button setBackgroundColor:[UIColor redColor]];
//添加事件
[button addTarget:self action:@selector(ActionContent:) forControlEvents:UIControlEventTouchUpInside];
//移除事件
[button removeTarget:self action:@selector(ActionContent:) forControlEvents:UIControlEventTouchUpInside];
/*
何时释放release UIButton?
是否在dealloc中对UIButton对象进行release操作,取决于UIButton初始化的方式。
如果使用[UIButtonbuttonWithType:UIButtonTypeRoundedRect]这种方式,是不需要进行release操作的,因为这种方式是自动释放的。如果使用 [[UIButton alloc]init]的方式,则需要主动进行release释放操作
*/
//添加到view视图
[self.view addSubview:button];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeInfoDark];
[btn2 setBackgroundColor:[UIColor redColor]];
[btn2 setTitle:@"张三" forState:UIControlStateNormal];
[btn2 setFrame:CGRectMake(50, 50, 100, 100)];
[self.view addSubview:btn2];UI-UIButton细说
最新推荐文章于 2022-05-30 17:48:28 发布
本文详细介绍了UIButton的各种样式和状态,包括不同背景风格和按钮风格的应用,并提供了如何设置按钮的标题、图片、背景颜色以及添加和移除事件响应的具体代码示例。
776

被折叠的 条评论
为什么被折叠?



