Button:多應(yīng)用于監(jiān)聽點(diǎn)擊,對(duì)想要獲取的事件響應(yīng)
? ? ? ? ? ? ? 內(nèi)部結(jié)構(gòu):ImageView:顯示圖片 -?Label:顯示文字
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?也可以當(dāng)作ImageView和Label的合體使用
?常見屬性:
? ? ? ? --UIButton狀態(tài):
? ? ? UIControlStateNormal ? ? ? ? //正常狀態(tài)
? ? ? UIControlStateHighlighted ?//高亮狀態(tài)
? ? ? UIControlStateDisable ? ? ? ??//禁用狀態(tài)
? ? ? UIControlStateSelected ? ? ? ?//選中狀態(tài)
? ? ? UIControlStateReserved ? ? ? //保留狀態(tài)
? ? ? ? ? ? --UIButton類型
? ? ? UIButtonTypeCustom? ? ? ? //自定義類型,多用于當(dāng)有圖片的時(shí)候
? ? ? UIButtonTypeContactAdd ? // 添加按鈕铜幽,不用設(shè)置Frame,有默認(rèn)值
? ? ? UIButtonTypeInfoDark? ? ? ? //暗色背景的信息按鈕
? ? ? UIButtonTypeInfoLight ? ? ? ?//淺色背景的信息按鈕
? ? ? ?注意: 一個(gè)方法可以監(jiān)聽多個(gè)按鈕;
? ? ? ? ?--UIButton常用方法
? ? ? ? ? ?//設(shè)置對(duì)應(yīng)狀態(tài)的標(biāo)題內(nèi)容
?- (void)setTitle:(NSString*)title forState:(UIControlState)state;
? ? ? ? ? ?//設(shè)置對(duì)應(yīng)狀態(tài)的標(biāo)題顏色
?-(void)setTitleColor:(UIColor *)color forState:(UIControlState)state;
? ? ? ? ?//設(shè)置對(duì)應(yīng)狀態(tài)的標(biāo)題陰影顏色?
? -(void)setTitleShadowColor:(UIColor *)color forState:(UIControlState)state;
? ?//設(shè)置對(duì)應(yīng)狀態(tài)的按鈕的圖片
? - (void)setImage:(UIImage*)image forState:(UIControlState)state;
? ? ? ? ? ? //設(shè)置對(duì)應(yīng)狀態(tài)的按鈕背景圖片
? -(void)setBackgroundImage:(UIImage *)image forState:(UIControlState)state
? ? ? ? ?//添加事件;用代碼的方式監(jiān)聽點(diǎn)擊事件
? - (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents;
--自定義button設(shè)置圖片大小昙读,文字大小
? ? 方法1-//重寫設(shè)置圖片的的位置
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloatwight = contentRect.size.width;
CGFloathight = contentRect.size.height;
returnCGRectMake(5, 5, wight/2,hight/2);
}
//重寫次方法設(shè)置文字的的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloatwight = contentRect.size.width;
CGFloathight = contentRect.size.height;
returnCGRectMake(wight/2, 5, wight/2, hight/2);
}
方法2- 重寫layousubviws設(shè)置圖片文字位置
- (void)layoutSubviews
{
[superlayoutSubviews];
CGFloatwight =self.frame.size.width;
CGFloathight =self.frame.size.height;
self.imageView.frame=CGRectMake(5, 5, wight/2, wight/2);
}
上述兩種方法,多用于自定義控件;合理利用button的label和imgview
當(dāng)自定義button的時(shí)候:but.imageView.frame=CGRectMake(0, 0, 80, 40);??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //?錯(cuò)誤宇立,設(shè)置沒有效果
--設(shè)置按鈕內(nèi)邊距(圖片-文字-上下左右的邊距 )
1.通過storyboard的Edgt設(shè)置 圖片-文字 內(nèi)邊距
2.通過代碼
//設(shè)置圖片的邊距
self.btn.titleEdgeInsets=UIEdgeInsetsMake(10, 20, 30, 40);
//設(shè)置label的編劇
self.btn.imageEdgeInsets=UIEdgeInsetsMake(10, 20, 30, 40);