您的位置:首页 > 产品设计 > UI/UE

iOS开发——UI基础-按钮的创建和设置

2015-07-15 02:52 441 查看
@interface ViewController ()
- (IBAction)customBtnClick;

@end

@implementation ViewController

- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.

UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];

// 1.设置标题
//    btn.titleLabel.text = @"我是按钮"; // 千万不要这样写
// 一般情况下给按钮设置内容都是setXXX
[btn setTitle:@"我是按钮" forState:UIControlStateNormal];
[btn setTitle:@"哥是高亮" forState:UIControlStateHighlighted];

// 2.设置标题颜色
//    btn.backgroundColor = [UIColor redColor];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

// 3.设置图标
[btn setImage:[UIImage imageNamed:@"common_icon_check"] forState:UIControlStateNormal];

// 4.设置背景图片
[btn setBackgroundImage:[UIImage imageNamed:@"common_button_big_blue_highlighted"] forState:UIControlStateNormal];

// 5.监听按钮的点击
// Target:让谁监听按钮
// action:监听到之后需要执行的方法
// Events:事件的类型
// 规律: 只要是继承于UIControl的控件, 都可以通过addTarget来添加监听
[btn addTarget:self action:@selector(customBtnClick) forControlEvents:UIControlEventTouchUpInside];

//    UISwitch sw = nil;
//    [sw addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]

//    UISegmentedControl *sc = nil;
//    [sc addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]

//    UITextField *tf = nil;
//    [tf addTarget:<#(nullable id)#> action:<#(nonnull SEL)#> forControlEvents:<#(UIControlEvents)#>]

btn.titleLabel.font = [UIFont systemFontOfSize:14];
btn.frame = CGRectMake(100, 100, 100, 100);
[self.view addSubview:btn];
}

- (IBAction)customBtnClick {
NSLog(@"%s", __func__);
}
@end




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: