Button:多應用于監聽點擊,對想要獲取的事件響應
? ? ? ? ? ? ? 內部結構:ImageView:顯示圖片 -?Label:顯示文字
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?也可以當作ImageView和Label的合體使用
?常見屬性:
? ? ? ? --UIButton狀態:
? ? ? UIControlStateNormal ? ? ? ? //正常狀態
? ? ? UIControlStateHighlighted ?//高亮狀態
? ? ? UIControlStateDisable ? ? ? ??//禁用狀態
? ? ? UIControlStateSelected ? ? ? ?//選中狀態
? ? ? UIControlStateReserved ? ? ? //保留狀態
? ? ? ? ? ? --UIButton類型
? ? ? UIButtonTypeCustom? ? ? ? //自定義類型,多用于當有圖片的時候
? ? ? UIButtonTypeContactAdd ? // 添加按鈕,不用設置Frame,有默認值
? ? ? UIButtonTypeInfoDark? ? ? ? //暗色背景的信息按鈕
? ? ? UIButtonTypeInfoLight ? ? ? ?//淺色背景的信息按鈕
? ? ? ?注意: 一個方法可以監聽多個按鈕;
? ? ? ? ?--UIButton常用方法
? ? ? ? ? ?//設置對應狀態的標題內容
?- (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;
--自定義button設置圖片大小,文字大小
? ? 方法1-//重寫設置圖片的的位置
- (CGRect)imageRectForContentRect:(CGRect)contentRect
{
CGFloatwight = contentRect.size.width;
CGFloathight = contentRect.size.height;
returnCGRectMake(5, 5, wight/2,hight/2);
}
//重寫次方法設置文字的的位置
- (CGRect)titleRectForContentRect:(CGRect)contentRect
{
CGFloatwight = contentRect.size.width;
CGFloathight = contentRect.size.height;
returnCGRectMake(wight/2, 5, wight/2, hight/2);
}
方法2- 重寫layousubviws設置圖片文字位置
- (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
當自定義button的時候:but.imageView.frame=CGRectMake(0, 0, 80, 40);??
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //?錯誤,設置沒有效果
--設置按鈕內邊距(圖片-文字-上下左右的邊距 )
1.通過storyboard的Edgt設置 圖片-文字 內邊距
2.通過代碼
//設置圖片的邊距
self.btn.titleEdgeInsets=UIEdgeInsetsMake(10, 20, 30, 40);
//設置label的編劇
self.btn.imageEdgeInsets=UIEdgeInsetsMake(10, 20, 30, 40);