iOS按鈕相關設置 (uibutton)

  1. //login button  
  2.     //  .h 中定義  
  3.     UIButton *_loginBtn;  
  4.     @property (strong,nonatomic)UIButton *loginBtn;  
  5.       
  6.       
  7.     // .m 中實現設置按鈕  
  8.     @synthesize loginBtn = _loginBtn;//使用備份變量名  
  9.       
  10.     //設置按鈕的  形狀  
  11.     self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
  12.     /* 
  13.      buttonWithType:  定義button按鈕的外形 
  14.      六種定義button類型: 下面有圖解 
  15.      UIButtonTypeCustom = 0,    無類型 
  16.      UIButtonTypeRoundedRect,    四個角是圓弧   型的 
  17.      UIButtonTypeDetailDisclosure, 
  18.      UIButtonTypeInfoLight, 
  19.      UIButtonTypeInfoDark, 
  20.      UIButtonTypeContactAdd, 
  21.      */  
  22.       
  23.     //定義button按鈕在frame上的座標(位置),和這個按鈕的寬/高  
  24.     self.loginBtn.frame = CGRectMake(40, 200, 80, 30);  
  25.       
  26.       
  27.     [self.loginBtn setTitle:@"Login" forState:UIControlStateNormal];  
  28.     /* 
  29.      常用的屬性: 
  30.       setTitle:  設置button按鈕的名稱 
  31.       setImage: [UIImage imageNamed:@"圖名"]  添加圖片 
  32.       setTitleColor:[UIColor redColor]  設置字體顏色 
  33.       
  34.      forState 設置 按鈕點擊前後的狀態   : 下有圖解 
  35.      UIControlStateHighlighted 
  36.      UIControlStateSelected 
  37.      UIControlStateDisabled 
  38.      UIControlStateNormal 
  39.       
  40.      */  
  41.       
  42.     // 爲按鈕添加一個動作  
  43.     //  action:  如果點擊的話執行的方法  
  44.     [self.loginBtn addTarget:self action:@selector(Login:) forControlEvents:UIControlEventTouchUpInside];  
  45.       
  46.     //把button控件添加到view中顯示  
  47.     [self.view addSubview:self.loginBtn];  

  1. //執行動作的方法  
  2. -(IBAction)Login:(id)sender;  


六種定義button類型: 

     UIButtonTypeCustom = 0,   無類型

     UIButtonTypeRoundedRect,   四個角是圓弧  型的   


     UIButtonTypeDetailDisclosure    

     UIButtonTypeInfoLight    


     UIButtonTypeInfoDark    


     UIButtonTypeContactAdd    





forState 設置 按鈕點擊前後的狀態   

        點擊前                                         點擊後

UIControlStateHighlighted


UIControlStateSelected   


UIControlStateDisabled   


      UIControlStateNormal     



UIButtonTypeRoundedRect 設置爲這個屬性,是可以滿足我們普通情況下的按鈕圓角,當我們在button上添加背景圖片和背景顏色的時候就會發現,這個屬性並不適用,因爲現在的button已經不是圓角的了,它顯示的是圖片的形狀,當設置背景顏色設置爲UIButtonTypeCustom屬性纔可以顯示出來。所以我們需要用UIButton控件的其它屬性來滿足我們的需求

  1. UIButton *btn;  
  2.     [btn.layer setMasksToBounds:YES];  
  3.     [btn.layer setCornerRadius:10.0];//設置矩形四個圓角半徑  
  4.       
  5.     /* 
  6.         [btn.layer setBorderWidth:1.0];//邊框寬度 
  7.      */  






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章