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