創(chuàng)建
UIButton * btn = [[UIButton alloc] init];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // 自定義按鈕
UIButtonTypeSystem NS_ENUM_AVAILABLE_IOS(7_0), // 系統(tǒng)按鈕 iOS7以后圓角按鈕被 UIButtonTypeSystem 取代 兩者等同
UIButtonTypeDetailDisclosure, // 藍(lán)色箭頭按鈕(用作詳
UIButtonTypeInfoLight, // 亮色感嘆號按鈕
UIButtonTypeInfoDark, // 深色感嘆號按鈕
UIButtonTypeContactAdd, // 加號按鈕
UIButtonTypeRoundedRect = UIButtonTypeSystem, // Deprecated, use UIButtonTypeSystem instead
};
狀態(tài)
typedef NS_OPTIONS(NSUInteger, UIControlState) {
UIControlStateNormal = 0, // 正常狀態(tài)
UIControlStateHighlighted = 1 << 0, // 高亮狀態(tài)
UIControlStateDisabled = 1 << 1, // 失效狀態(tài)
UIControlStateSelected = 1 << 2, // 選中狀態(tài)
UIControlStateFocused NS_ENUM_AVAILABLE_IOS(9_0) = 1 << 3, // 聚焦?fàn)顟B(tài) (iOS新加入 應(yīng)該和3D Touch有關(guān))
UIControlStateApplication = 0x00FF0000, // 當(dāng)用做應(yīng)用標(biāo)志時(shí)
UIControlStateReserved = 0xFF000000 // 框架預(yù)留 無意義
};
屬性
// frame
btn.frame = CGRectMake(20, 20, 70, 30);
// 背景色
btn.backgroundColor = [UIColor redColor];
// 標(biāo)題
[btn setTitle:@"按鈕" forState:UIControlStateNormal]; // 標(biāo)題
[btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; // 標(biāo)題顏色
[btn setTitleShadowColor:[UIColor redColor] forState:UIControlStateNormal]; // 標(biāo)題陰影顏色
// 圖片
[btn setImage:[UIImage imageNamed:@"btnIcon"] forState:UIControlStateNormal]; // 不會(huì)被拉伸浅浮,原比例顯示
[btn setBackgroundImage:[UIImage imageNamed:@"btnBgImg"] forState:UIControlStateNormal]; // 會(huì)被拉伸照藻,充滿整個(gè)btn
// 間距
btn.contentEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // btn整體內(nèi)容四周的間距
btn.titleEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // 標(biāo)題四周間距
btn.imageEdgeInsets = UIEdgeInsetsMake(5, 5, 5, 5); // 圖片四周間距
// 設(shè)置高亮狀態(tài) 加深圖片顏色
btn.adjustsImageWhenHighlighted = NO; // 默認(rèn)為YES 設(shè)置為NO取消效果
// 設(shè)置失效狀態(tài) 加深圖片顏色
btn.adjustsImageWhenDisabled = NO; // 默認(rèn)為YES 設(shè)置NO取消效果
// 設(shè)置高亮狀態(tài) 按鈕發(fā)光
btn.showsTouchWhenHighlighted = YES; // 默認(rèn)為NO
// 設(shè)置高亮狀態(tài) 是否改變陰影
btn. reversesTitleShadowWhenHighlighted = YES; // 默認(rèn)為NO
事件
// 添加事件
[btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
// 事件狀態(tài)
typedef NS_OPTIONS(NSUInteger, UIControlEvents) {
UIControlEventTouchDown = 1 << 0, // 按下
UIControlEventTouchDownRepeat = 1 << 1, // 多次按下
UIControlEventTouchDragInside = 1 << 2, // 保持按下,在按鈕及其一定的外圍拖動(dòng)
UIControlEventTouchDragOutside = 1 << 3, // 保持按下,在按鈕外面拖動(dòng)
UIControlEventTouchDragEnter = 1 << 4, // DragOutside進(jìn)入DragInside觸發(fā)
UIControlEventTouchDragExit = 1 << 5, // DragInside到DragOutside觸發(fā)
UIControlEventTouchUpInside = 1 << 6, // 按鈕及其一定外圍內(nèi)松開
UIControlEventTouchUpOutside = 1 << 7, // 按鈕外面松開
UIControlEventTouchCancel = 1 << 8, // 點(diǎn)擊取消
... ...
}
獲取
@property(nullable, nonatomic,readonly,strong) NSString *currentTitle; // 當(dāng)前標(biāo)題
@property(nonatomic,readonly,strong) UIColor *currentTitleColor; // 當(dāng)前標(biāo)題顏色
@property(nullable, nonatomic,readonly,strong) UIColor *currentTitleShadowColor; // 當(dāng)前標(biāo)題陰影顏色
@property(nullable, nonatomic,readonly,strong) UIImage *currentImage; // 當(dāng)前圖片
@property(nullable, nonatomic,readonly,strong) UIImage *currentBackgroundImage; // 當(dāng)前背景圖片
@property(nullable, nonatomic,readonly,strong) NSAttributedString *currentAttributedTitle NS_AVAILABLE_IOS(6_0); // 當(dāng)前富文本標(biāo)題
@property(nullable, nonatomic,readonly,strong) UILabel *titleLabel NS_AVAILABLE_IOS(3_0); // 當(dāng)前標(biāo)題UILabel
@property(nullable, nonatomic,readonly,strong) UIImageView *imageView NS_AVAILABLE_IOS(3_0); // 當(dāng)前圖片UIImageView
- (CGRect)backgroundRectForBounds:(CGRect)bounds; // 返回背景繪制區(qū)域
- (CGRect)contentRectForBounds:(CGRect)bounds; // 返回內(nèi)容繪制區(qū)域
- (CGRect)titleRectForContentRect:(CGRect)contentRect; // 返回標(biāo)題繪制區(qū)域
- (CGRect)imageRectForContentRect:(CGRect)contentRect; // 返回圖片繪制區(qū)域
版權(quán)聲明:出自MajorLMJ技術(shù)博客的原創(chuàng)作品 撇眯,轉(zhuǎn)載時(shí)必須注明出處及相應(yīng)鏈接!