ios11--UIButton


//
//  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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM