標籤:with icon 背景圖 title ram touch 1.4 cti gen
//// ViewController.m// 02-UIButton(在代碼中使用)//#import "ViewController.h"@interface ViewController ()@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; // 1.1 建立按鈕對象// UIButton *button = [[UIButton alloc] init]; // 注意:設定按鈕的類型只能在初始化的時候設定 -> UIButtonTypeCustom UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; // 1.2 設定按鈕的類型,是一個枚舉, //button.buttonType = UIButtonTypeInfoDark; // 1.3 設定frame button.frame = CGRectMake(100, 100, 170, 60); // 1.4 設定背景顏色// button.backgroundColor = [UIColor redColor];// [button setBackgroundColor:[UIColor redColor]]; // 1.5 設定文字 // 分狀態的:// button.titleLabel.text = @"普通文字"; 顯示不出來 [button setTitle:@"普通按鈕" forState:UIControlStateNormal]; //正常顯示的文字 [button setTitle:@"高亮按鈕" forState:UIControlStateHighlighted];//點擊時的文字 // 1.6 設定文字的顏色 [button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal]; [button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted]; // 1.7 設定文字的陰影顏色 [button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal]; [button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted]; button.titleLabel.shadowOffset = CGSizeMake(3, 2); // 1.8 設定內容圖片,圖片拖到Assets.xcassets右邊裡面去, [button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal]; [button setImage:[UIImage imageNamed:@"player_btn_pause_highlight"] forState:UIControlStateHighlighted]; button.imageView.backgroundColor = [UIColor purpleColor]; // 1.9 設定背景圖片 [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal]; [button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted]; // 2.0 加到控制器的view中 [self.view addSubview:button]; // 非常重要 /** * 監聽按鈕的點擊事件, * Target: 目標 (讓誰做事情) * action: 方法 (做什麼事情-->方法) * Events: 事件 */// SEL sel = @selector(clickButton:); [button addTarget:self action:@selector(demo:) forControlEvents:UIControlEventTouchUpInside];}- (void)demo:(UIButton *)btn{//btn就是按鈕, NSLog(@"%@", btn);}- (IBAction)clickButton:(UIButton *)button { button.enabled = NO;}@end
ios11--UIButton