UIButton控件是應用界面中經常使用的一個控件,用法總結:函數
1、初始化atom
UIButton的初始化通常使用其類方法,+ (id)buttonWithType:(UIButtonType)buttonType;code
風格的枚舉以下:繼承
typedef NS_ENUM(NSInteger, UIButtonType) { //用戶自定義,無風格 UIButtonTypeCustom = 0, //系統默認風格 UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), //一下這三種建立出來的按鈕同樣,一個藍色的圓圈,中間有個歎號 UIButtonTypeDetailDisclosure, UIButtonTypeInfoLight, UIButtonTypeInfoDark, //建立+號按鈕 UIButtonTypeContactAdd, //廢棄 UIButtonTypeRoundedRect = UIButtonTypeSystem, };
2、屬性設置事件
@property(nonatomic) UIEdgeInsets contentEdgeInsets UI_APPEARANCE_SELECTOR; //這個屬性設置button裏內容的偏移量,包括title和image,能夠用以下方法設置 btn.contentEdgeInsets=UIEdgeInsetsMake(20, 20, 0, 0); @property(nonatomic) UIEdgeInsets titleEdgeInsets; //這個屬性設置標題的偏移量 @property(nonatomic) BOOL reversesTitleShadowWhenHighlighted; //按鈕高亮時,是否改變陰影效果 @property(nonatomic) UIEdgeInsets imageEdgeInsets; //圖片的偏移量 @property(nonatomic)BOOL adjustsImageWhenHighlighted; //設置圖片的繪製是否高亮時變暗 @property(nonatomic)BOOL adjustsImageWhenDisabled; //設置圖片是否輕繪製當按鈕禁用時 @property(nonatomic)BOOL showsTouchWhenHighlighted; //設置是否顯示手指印在按鈕高亮的時候 @property(nonatomic,retain) UIColor *tintColor NS_AVAILABLE_IOS(5_0); //這個屬性會做用於標題和圖片,可是若是你是自定義風格的按鈕,這個屬性將不起任何做用,它只做用於系統的 @property(nonatomic,readonly) UIButtonType buttonType; //設置button的風格
3、一些set方法圖片
- (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)setAttributedTitle:(NSAttributedString *)title forState:(UIControlState)state NS_AVAILABLE_IOS(6_0); //經過AttributeString建立標題
注意:按鈕圖片設置和背景圖片的不一樣在於:get
一、設置圖片,若是有標題會和標題並列顯示it
二、設置背景圖片會出如今標題下面方法
三、圖片的偏移量能夠設置,背景圖片不能夠。im
4、一些get方法,能夠獲得上述設置的屬性
- (NSString *)titleForState:(UIControlState)state; - (UIColor *)titleColorForState:(UIControlState)state; - (UIColor *)titleShadowColorForState:(UIControlState)state; - (UIImage *)imageForState:(UIControlState)state; - (UIImage *)backgroundImageForState:(UIControlState)state; - (NSAttributedString *)attributedTitleForState:(UIControlState)state NS_AVAILABLE_IOS(6_0);
5、一些只讀屬性
@property(nonatomic,readonly,retain) NSString *currentTitle; @property(nonatomic,readonly,retain) UIColor *currentTitleColor; @property(nonatomic,readonly,retain) UIColor *currentTitleShadowColor; @property(nonatomic,readonly,retain) UIImage *currentImage; @property(nonatomic,readonly,retain) UIImage *currentBackgroundImage; @property(nonatomic,readonly,retain) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); //這兩個參數須要注意,雖然他們是隻讀屬性不能從新設置,可是咱們能夠設置label和imageView的相關屬性 @property(nonatomic,readonly,retain) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); @property(nonatomic,readonly,retain) UIImageView *imageView NS_AVAILABLE_IOS(3_0);
6、下面這些函數,都會返回一個CGRect 矩形範圍
- (CGRect)backgroundRectForBounds:(CGRect)bounds; //返回背景大小 - (CGRect)contentRectForBounds:(CGRect)bounds; //返回視圖大小,包括標題和圖片 - (CGRect)titleRectForContentRect:(CGRect)contentRect; //返回標題大小 - (CGRect)imageRectForContentRect:(CGRect)contentRect; //返回圖片大小
關於觸發事件,button是繼承於UIControl,這裏再也不敘述。
專一技術,熱愛生活,交流技術,也作朋友。
——琿少 QQ羣:203317592