UI-UIButton细说

本文详细介绍了UIButton的各种样式和状态,包括不同背景风格和按钮风格的应用,并提供了如何设置按钮的标题、图片、背景颜色以及添加和移除事件响应的具体代码示例。
 /*
     
     背景风格
     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];

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值