UIButton

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);

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 前言:UI控件整理之UIButton 一、顯示圖片(復選框) UIButton *button = [UIButt...
    心如止水的魚閱讀 293評論 0 0
  • 一個UIButton的實例變量, 使一個按鈕(button)在觸摸屏上生效。一個按鈕監聽觸摸事件,當被點擊時,給目...
    wushuputi閱讀 1,518評論 0 1
  • 字數1443 閱讀548 評論5 喜歡36UIButton 的全面解析建議收藏,用到的時候來這里一查就都明白了 初...
    xiao小馬哥閱讀 1,232評論 0 1
  • 概述 UIButton的父類是UIControl,UIControl的父類是UIView,UIView的父類是UI...
    guaker閱讀 2,815評論 1 9
  • Button的基本使用 既能顯示文字,又能顯示圖片 UIButton的狀態:normal(普通狀態): UICon...
    CoderRH閱讀 325評論 0 0