UIButton的详细使用

UIButton按钮是IOS开发中最常用的控件,作为IOS基础学习教程知识 ,初学者需要了解其基本定义和常用设置,以便在开发在熟练运用。

  第一、UIButton的定义

  UIButton *button=[[UIButton buttonWithType:(UIButtonType);

  能够定义的button类型有以下6种,

  typedef enum {

  UIButtonTypeCustom = 0, 自定义风格

  UIButtonTypeRoundedRect, 圆角矩形

  UIButtonTypeDetailDisclosure, 蓝色小箭头按钮,主要做详细说明用

  UIButtonTypeInfoLight, 亮色感叹号

  UIButtonTypeInfoDark, 暗色感叹号

  UIButtonTypeContactAdd, 十字加号按钮

  } UIButtonType;

  第二、设置frame

  button1.frame = CGRectMake(20, 20, 280, 40);

  [button setFrame:CGRectMake(20,20,50,50)];

  第三、button背景色

  button1.backgroundColor = [UIColor clearColor];

  [button setBackgroundColor:[UIColor blueColor]];

  第四、state状态

  forState: 这个参数的作用是定义按钮的文字或图片在何种状态下才会显现

  enum {

  UIControlStateNormal = 0, 常规状态显现

  UIControlStateHighlighted = 1 << 0, 高亮状态显现

  UIControlStateDisabled = 1 << 1, 禁用的状态才会显现

  UIControlStateSelected = 1 << 2, 选中状态

  UIControlStateApplication = 0x00FF0000, 当应用程序标志时

  UIControlStateReserved = 0xFF000000 为内部框架预留,可以不管

  };

  @property(nonatomic,getter=isEnabled)BOOL enabled; // default is YES. if NO, ignores touch events and subclasses may draw differently

  @property(nonatomic,getter=isSelected)BOOL selected; // default is NO may be used by some subclasses or by application

  @property(nonatomic,getter=isHighlighted)BOOL highlighted;

  第五 、设置button填充图片和背景图片

  [buttonsetImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

  [buttonsetBackgroundImage:[UIImageimageNamed:@"checkmarkControllerIcon"]forState:UIControlStateNormal];

  第六、设置button标题和标题颜色

  [button1 setTitle:@"点击" forState:UIControlStateNormal];

  [buttonsetTitleColor:[UIColorredColor]forState:UIControlStateNormal];

  第七、设置按钮按下会发光

  button.showsTouchWhenHighlighted=NO;

  第八、添加或删除事件处理

  [button1 addTarget:self action:@selector(butClick:) forControlEvents:UIControlEventTouchUpInside];

  [btn removeTarget:nil action:nil forControlEvents:UIControlEventTouchUpInside];

  第九、 设置按钮内部图片间距和标题间距

  UIEdgeInsets insets; // 设置按钮内部图片间距

  insets.top = insets.bottom = insets.right = insets.left = 10;

  bt.contentEdgeInsets = insets;

  bt.titleEdgeInsets = insets; // 标题间距


使用UIButton代码

- (void)setupUI{  
    //Hello Button  
    UIButton *hellobutton = [UIButton buttonWithType:UIButtonTypeSystem];  
    [hellobutton setTitle:@"Hello" forState:UIControlStateNormal];  
    [hellobutton addTarget:self action:@selector(onHelloButtonClicked:) forControlEvents:UIControlEventTouchUpInside];  
    [self.view addSubview:hellobutton];  
    hellobutton.translatesAutoresizingMaskIntoConstraints = NO;  
    [self.view addConstraints:@[  
                               [NSLayoutConstraint constraintWithItem:hellobutton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:60.0],  
                               [NSLayoutConstraint constraintWithItem:hellobutton attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40.0],  
                               [NSLayoutConstraint constraintWithItem:hellobutton attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterX multiplier:1.0 constant:0],  
                               [NSLayoutConstraint constraintWithItem:hellobutton attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0]  
                               ]];  
}  
  
#pragma mark - Action  
- (void)onHelloButtonClicked:(id)sender{  
    NSLog(@"Hello, world!");  
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Hello" message:@"Hello, World!" preferredStyle:UIAlertControllerStyleAlert];  
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){  
        NSLog(@"Cancel Action");  
    }];  
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){  
        NSLog(@"Ok Action");  
    }];  
    [alertController addAction:cancelAction];  
    [alertController addAction:okAction];  
    [self presentViewController:alertController animated:YES completion:nil];  
      
}  


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值