typedef NS_ENUM(NSInteger, UIButtonType) {
UIButtonTypeCustom = 0, // 自定義:無預設樣式,最靈活
UIButtonTypeRoundedRect, // 圓角矩形 (iOS 7+ 實為 System 類型)
UIButtonTypeDetailDisclosure, // 藍色箭頭 (>) 資訊揭露
UIButtonTypeInfoLight, // 淺色感嘆號 (i) 資訊按鈕
UIButtonTypeInfoDark, // 深色感嘆號 (i)
UIButtonTypeContactAdd, // 綠色加號 (+) 新增聯絡人
UIButtonTypeSystem = UIButtonTypeRoundedRect // 別名
};
創建:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem]; // 推薦類方法
// 或
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectZero]; // 預設 Custom
// Frame 佈局
btn.frame = CGRectMake(20, 100, 280, 44);
// Auto Layout (代碼)
btn.translatesAutoresizingMaskIntoConstraints = NO;
[NSLayoutConstraint activateConstraints:@[
btn.leadingAnchor.constraintEqualToAnchor(view.leadingAnchor, constant: 20),
btn.trailingAnchor.constraintEqualToAnchor(view.trailingAnchor, constant: -20),
btn.topAnchor.constraintEqualToAnchor(view.topAnchor, constant: 100),
btn.heightAnchor.constraintEqualToConstant(44)
]];
按鈕外觀隨**狀態組合**變化。常用狀態:
| 常數 | 值 | 觸發條件 |
|---|---|---|
| UIControlStateNormal | 0 | 正常、啟用、未選中、未高亮 |
| UIControlStateHighlighted | 1 << 0 | 手指按下未抬起 |
| UIControlStateDisabled | 1 << 1 | `enabled = NO` |
| UIControlStateSelected | 1 << 2 | `selected = YES` |
| UIControlStateApplication | 0x00FF0000 | 應用自定義狀態位 |
| UIControlStateReserved | 0xFF000000 | 系統保留 |
組合狀態:`Highlighted | Selected`、 `Disabled | Selected` 等。設置屬性時**最具體狀態優先**。
// 文字
[btn setTitle:@"正常標題" forState:UIControlStateNormal];
[btn setTitle:@"按下中..." forState:UIControlStateHighlighted];
[btn setTitle:@"不可用" forState:UIControlStateDisabled];
// 文字顏色
[btn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateHighlighted];
[btn setTitleColor:[UIColor grayColor] forState:UIControlStateDisabled];
// 富文本標題 (iOS 6+)
[btn setAttributedTitle:attrStr forState:UIControlStateNormal];
| 屬性 | 方法 | 特點 | 典型用途 |
|---|---|---|---|
| Image (前景圖) | setImage:forState: | 不拉伸、居中顯示、疊加在標題上方 | Icon、箭頭、勾選標記 |
| Background Image (背景圖) | setBackgroundImage:forState: | 拉伸填滿 (支援 capInsets)、置於最底層 | 按鈕背景、漸變、九宮格圖 |
// 前景 Icon
[btn setImage:[UIImage imageNamed:@"icon_star"] forState:UIControlStateNormal];
// 背景圖 (九宮格拉伸)
UIImage *bg = [[UIImage imageNamed:@"btn_bg"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[btn setBackgroundImage:bg forState:UIControlStateNormal];
UIImage *bgHL = [[UIImage imageNamed:@"btn_bg_hl"] resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
[btn setBackgroundImage:bgHL forState:UIControlStateHighlighted];
// 系統自動疊加高亮遮罩 (System 類型按鈕)
btn.showsTouchWhenHighlighted = YES; // 任何類型按鈕都可開啟白色閃爍效果
// 自定義高亮:用 Background Image 不同狀態圖片 (推薦)
// 或調整 alpha
btn.adjustsImageWhenHighlighted = YES; // 預設 YES,Image 也會疊加高亮
// 添加
[btn addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// 移除 (精確)
[btn removeTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
// 移除所有
[btn removeTarget:nil action:nil forControlEvents:UIControlEventAllEvents];
// 常用事件
UIControlEventTouchDown // 手指按下
UIControlEventTouchUpInside // 手指在內抬起 (標準點擊)
UIControlEventTouchUpOutside // 手指在外抬起 (取消)
UIControlEventTouchDragInside // 按下拖動在內
UIControlEventValueChanged // UISwitch/UISlider 等值改變
UIControlEventEditingDidBegin // 文字輸入開始
// 內容整體邊距 (圖片+文字作為整體)
UIEdgeInsets insets = UIEdgeInsetsMake(10, 15, 10, 15); // 上左下右
btn.contentEdgeInsets = insets;
// 圖片與文字相對位置微調
btn.titleEdgeInsets = UIEdgeInsetsMake(0, 8, 0, -8); // 文字右移 8pt
btn.imageEdgeInsets = UIEdgeInsetsMake(0, -8, 0, 8); // 圖片左移 8pt
// 圖文相對位置:imageLeft/right/top/bottom 需配合 imageEdgeInsets/titleEdgeInsets 計算
// 推薦:使用系統 System 類型按鈕 + image + title,自動佈局
// UIButton+Layout.h
@interface UIButton (Layout)
- (void)setImageLeftTextRight:(CGFloat)spacing;
- (void)setImageRightTextLeft:(CGFloat)spacing;
- (void)setImageTopTextBottom:(CGFloat)spacing;
- (void)setImageBottomTextTop:(CGFloat)spacing;
@end
// 實現核心邏輯 (以 ImageLeft 為例)
- (void)setImageLeftTextRight:(CGFloat)spacing {
self.titleEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, -spacing/2);
self.imageEdgeInsets = UIEdgeInsetsMake(0, -spacing/2, 0, spacing/2);
self.contentEdgeInsets = UIEdgeInsetsMake(0, spacing/2, 0, spacing/2);
}
let button = UIButton(type: .system) // 或 .custom
button.setTitle("登入", for: .normal)
button.setTitleColor(.white, for: .normal)
button.setTitleColor(.lightGray, for: .highlighted)
button.titleLabel?.font = .systemFont(ofSize: 18, weight: .medium)
button.backgroundColor = .systemBlue
button.layer.cornerRadius = 8
button.clipsToBounds = true
// 圖文佈局 (iOS 15+ 原生支援)
if #available(iOS 15.0, *) {
var config = UIButton.Configuration.filled()
config.title = "登入"
config.image = UIImage(systemName: "arrow.right")
config.imagePlacement = .trailing
config.imagePadding = 8
config.baseForegroundColor = .white
config.baseBackgroundColor = .systemBlue
config.cornerStyle = .medium
button.configuration = config
} else {
// 降級處理
}
button.addAction(UIAction { _ in
print("按鈕點擊")
}, for: .touchUpInside)