- //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];//邊框寬度
- */