按鈕的建立和設定

來源:互聯網
上載者:User

標籤:

 1 @interface ViewController () 2 - (IBAction)customBtnClick; 3  4 @end 5  6 @implementation ViewController 7  8 - (void)viewDidLoad { 9     [super viewDidLoad];10     // Do any additional setup after loading the view, typically from a nib.11     12     UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];13     14     // 1.設定標題15 //    btn.titleLabel.text = @"我是按鈕"; // 千萬不要這樣寫16     // 一般情況下給按鈕設定內容都是setXXX17     [btn setTitle:@"我是按鈕" forState:UIControlStateNormal];18     [btn setTitle:@"哥是高亮" forState:UIControlStateHighlighted];19     20     // 2.設定標題顏色21 //    btn.backgroundColor = [UIColor redColor];22     [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];23     24     // 3.設定表徵圖25     [btn setImage:[UIImage imageNamed:@"common_icon_check"] forState:UIControlStateNormal];26     27     // 4.設定背景圖片28     [btn setBackgroundImage:[UIImage imageNamed:@"common_button_big_blue_highlighted"] forState:UIControlStateNormal];29     30     // 5.監聽按鈕的點擊31     // Target:讓誰監聽按鈕32     // action:監聽到之後需要執行的方法33     // Events:事件的類型34     // 規律: 只要是繼承於UIControl的控制項, 都可以通過addTarget來添加監聽35     [btn addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];36     37 //    UISwitch sw = nil;38 //    [sw addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]39     40 //    UISegmentedControl *sc = nil;41 //    [sc addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]42     43 //    UITextField *tf = nil;44 //    [tf addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]45     46     btn.titleLabel.font = [UIFont systemFontOfSize:14];47     btn.frame = CGRectMake(100, 100, 100, 100);48     [self.view addSubview:btn];49 }50 51 - (IBAction)customBtnClick {52     NSLog(@"%s", __func__);53 }54 @end

按鈕的建立和設定

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.