UIButton的用法


/*
     1.样式:
     2.状态:UIControlStateNormal=0  常规状态显现
     UIControlStateHighlighted=1<<0高亮状态显现
    */
    UIButton  *button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(170, 200, 70, 70);
    //button.backgroundColor=[UIColor blackColor];
   
    //设置常规标题
    [button setTitle:@"登录" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //设置button上的字体大小
    button.titleLabel.font=[UIFont systemFontOfSize:18];

    //设置高亮标题
//    [button setTitle:@"高亮" forState:UIControlStateHighlighted];
//    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    //按钮是否禁用
//    button.enabled=NO;
//    [button setTitle:@"禁用" forState:UIControlStateDisabled];
//    [button setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
    //判断按钮是否选中
//    button.selected=YES;
    [button setTitle:@"被选中" forState:UIControlStateSelected];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateSelected];
    //设置圆角
    button.layer.cornerRadius=50;
    button.layer.masksToBounds=YES;
    //设置背景图片
    //UIImage *image=[UIImage imageNamed:@"Icon-80.png"];
    //[button setBackgroundImage:image forState:UIControlStateNormal];
    UIImage *image1=[UIImage imageNamed:@"649417.jpg"];
   [button setBackgroundImage:image1 forState:UIControlStateSelected];
//    [button setImage:image forState:UIControlStateNormal];
    //[button setImage:image1 forState:UIControlStateHighlighted];
    //一点就会高亮
   // button.showsTouchWhenHighlighted=YES;
    //@selector(selector)方法选择器
    //监听
    [button addTarget:self action:@selector(text:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(text1:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
    view.backgroundColor=[UIColor yellowColor];
    view.tag=110;
    [self.view addSubview:view];
   

}
-(void)text1:(UIButton *)button{
    if (button.selected==NO) {
        button.selected=YES;
       

    }else{
        button.selected=NO;
    }
}
-(void)text:(UIButton *)sender{
    UIView *view=[self.view viewWithTag:110];
    view.backgroundColor=[UIColor colorWithRed:(arc4random()%(256-0+1)+0)/256.0 green:(arc4random()%(256-0+1)+0)/256.0 blue:(arc4random()%(256-0+1)+0)/256.0 alpha:1];
    view.frame=CGRectMake(arc4random()%(414-50-0+1)+0, arc4random()%(736-0+1)+0-50, arc4random()%(414-0+1)+0-50,50);

}

 

 

UIButton属性

1.UIButton状态:

UIControlStateNormal          // 正常状态   

UIControlStateHighlighted     // 高亮状态   

UIControlStateDisabled        // 禁用状态    

UIControlStateSelected        // 选中状态    

UIControlStateApplication     //     

UIControlStateReserved        // 保留状态

2.Uibutton类型:

UIButtonTypeCustom            //自定义类型

 添加图片:  灰色背景颜色:

UIButtonTypeRoundedRect       //圆角类型

 

UIButtonTypeDetailDisclosure   //细节展示按钮

 

UIButtonTypeInfoLight          //浅色背景的信息按钮

 

UIButtonTypeInfoDark           //暗色背景的信息按钮

 

UIButtonTypeContactAdd         // 添加按钮

 

3.UIButton常用属性

给按钮设置文字时,苹果文档说明,不能使用label对象设置文字的颜色或者阴影颜色,相反必须使用setTitleColor:forState: and setTitleShadowColor:forState:这两个方法才能修改这些值。

 

设置按钮中其他属性依次类推。。。。

 

//设置对应状态的标题内容default is nil. title is assumed to be single line

- (void)setTitle:(NSString *)title forState:(UIControlState)state;  

//设置对应状态的标题颜色           

- (void)setTitleColor:(UIColor *)color forState:(UIControlState)state;   

//设置对应状态的标题阴影颜色            

- (void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;          

//设置对应状态的按钮的图片

- (void)setImage:(UIImage *)image forState:(UIControlState)state;        

//设置对应状态的按钮背景图片

- (void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state;

 

添加事件

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;

这些事件都是基于触摸、基于值、基于编辑。如下事件会触发

在点击按钮是按钮是凹下去,然后弹起才触发起事件,按钮的状态有: 

  1. UIControlEventTouchDown      // 按下    
  2. UIControlEventTouchDownRepeat  多次按下   
  3. UIControlEventTouchUpInside // 在按及其一定外围内
  4. UIControlEventTouchUpOutside // 外面松  

 

4.adjustsImageWhenDisabled

当按钮禁用的情况下,图像的颜色会被画深一点,默认为YES

5.adjustsImageWhenHighlighted

当按钮高亮的情况下,图像的颜色会被画深一点,默认为YES

6.showsTouchWhenHighlighted

button.showsTouchWhenHighlighted=YES;点击时的闪光效果会被前景图片遮住中间部分;

6.contentEdgeInsets

设置按钮的内部内容(包含按钮图片和标题)离按钮边缘上下左右的距离。

 

7.按钮实例

1.有些候我UIButtontitle居左齐,我设置 
btn.textLabel.textAlignment = UITextAlignmentLeft

有作用的,我需要设置 

btn.contentHorizontalAlignment = UIControlContentHorizonAlignmentLeft;

但是问题又出,此文字会紧贴边框,我可以设置 

btn.contentEdgeInsets = UIEdgeInsetsMake(0,10, 0, 0);

使文字距离边框保持10个像素的距离。 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM