iOS--UIButton的使用方法

UIButton的常用方法

//初始化时设置Button样式,个人喜欢用圆角样式
UIButton * button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
//UIButton的几种样式
//typedef NS_ENUM(NSInteger, UIButtonType) {
//    UIButtonTypeCustom = 0,                         // //no button type
//    UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0),  // //standard system button
//    UIButtonTypeDetailDisclosure,
//    UIButtonTypeInfoLight,
//    UIButtonTypeInfoDark,
//    UIButtonTypeContactAdd,
//    UIButtonTypeRoundedRect = UIButtonTypeSystem,   // //Deprecated, use UIButtonTypeSystem instead
//};
//设置button的位置和大小
button.frame = CGRectMake(50, 50, 100, 30);
//设置button背景色
button.backgroundColor = [UIColor magentaColor];
//设置在什么状态下显示什么文字
[button setTitle:@"按钮" forState:UIControlStateNormal];
//设置在什么状态下字体什么颜色
[button setTitleColor:[UIColor cyanColor] forState:UIControlStateNormal];
//设置背景图片
[button setBackgroundImage:[UIImage imageNamed:@"image.jpg"] forState:UIControlStateNormal];
//UIControlState的样式
//    typedef NS_OPTIONS(NSUInteger, UIControlState) {
//        UIControlStateNormal       = 0,
//        UIControlStateHighlighted  = 1 << 0,                  // used when UIControl isHighlighted is set
//        UIControlStateDisabled     = 1 << 1,
//        UIControlStateSelected     = 1 << 2,                  // flag usable by app (see below)
//        UIControlStateApplication  = 0x00FF0000,              // additional flags available for application use
//        UIControlStateReserved     = 0xFF000000               // flags reserved for internal framework use
//    };
//给button添加响应事件
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
//添加到view
[self.view addSubview:button];
//button的响应事件
- (void)buttonAction:(UIButton*)sender {
    NSLog(@"Hello world");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值