標籤:
//login button // .h 中定義 UIButton *_loginBtn; @property (strong,nonatomic)UIButton *loginBtn; // .m 中實現設定按鈕 @synthesize loginBtn = _loginBtn;//使用備份變數名 //設定按鈕的 形狀 self.loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; /* buttonWithType: 定義button按鈕的外形 六種定義button類型: 下面有圖解 UIButtonTypeCustom = 0, 無類型 UIButtonTypeRoundedRect, 四個角是圓弧 型的 UIButtonTypeDetailDisclosure, UIButtonTypeInfoLight, UIButtonTypeInfoDark, UIButtonTypeContactAdd, */ //定義button按鈕在frame上的座標(位置),和這個按鈕的寬/高 self.loginBtn.frame = CGRectMake(40, 200, 80, 30); [self.loginBtn setTitle:@"Login" forState:UIControlStateNormal]; /* 常用的屬性: setTitle: 設定button按鈕的名稱 setImage: [UIImage imageNamed:@"圖名"] 添加圖片 setTitleColor:[UIColor redColor] 設定字型顏色 forState 設定 按鈕點擊前後的狀態 : 下有圖解 UIControlStateHighlighted UIControlStateSelected UIControlStateDisabled UIControlStateNormal */ // 為按鈕添加一個動作 // action: 如果點擊的話執行的方法 [self.loginBtn addTarget:self action:@selector(Login:) forControlEvents:UIControlEventTouchUpInside]; //把button控制項添加到view中顯示 [self.view addSubview:self.loginBtn];
//執行動作的方法-(IBAction)Login:(id)sender;
六種定義button類型:
UIButtonTypeCustom = 0, 無類型
UIButtonTypeRoundedRect, 四個角是圓弧 型的
UIButtonTypeDetailDisclosure
UIButtonTypeInfoLight
UIButtonTypeInfoDark
UIButtonTypeContactAdd
forState 設定 按鈕點擊前後的狀態
點擊前 點擊後
UIControlStateHighlighted
UIControlStateSelected
UIControlStateDisabled
UIControlStateNormal
UIButtonTypeRoundedRect 設定為這個屬性,是可以滿足我們普通情況下的按鈕圓角,當我們在button上添加背景圖片和背景顏色的時候就會發現,這個屬性並不適用,因為現在的button已經不是圓角的了,它顯示的是圖片的形狀,當設定背景顏色設定為UIButtonTypeCustom屬性才可以顯示出來。所以我們需要用UIButton控制項的其它屬性來滿足我們的需求
UIButton *btn; [btn.layer setMasksToBounds:YES]; [btn.layer setCornerRadius:10.0];//設定矩形四個圓角半徑 /* [btn.layer setBorderWidth:1.0];//邊框寬度 */
原文:http://blog.csdn.net/like7xiaoben/article/details/7588551
轉-UIButton定義和設定圓角