// ViewController.m
// UIButton1_addsub
//
// Created by cdj on 17/9/8.
// Copyright © 2017年 ue. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
//@property (weak, nonatomic) IBOutlet UIButton *btn;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//UIButton *button = [[UIButton alloc] init];
//button.buttonType = UIButtonTypeInfoDark;
//UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
//1.1新建一个自定义按键
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// 1.2设置按键位置和大小:x,y 宽,高
button.frame = CGRectMake(100, 100, 150, 40);
// 1.3设置背景色:两种方法都可以
//button.backgroundColor = [UIColor redColor];
[button setBackgroundColor:[UIColor redColor]];
// 1.4设置文字;分状态
//button.titleLabel.text = @"普通"; //只读无法设置
[button setTitle:@"普通" forState:UIControlStateNormal];
[button setTitle:@"高亮" forState:UIControlStateHighlighted];
// 1.5设置文字颜色
[button setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor yellowColor] forState:UIControlStateHighlighted];
// 1.6设置文字阴影及偏移量
[button setTitleShadowColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleShadowColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
button.titleLabel.shadowOffset = CGSizeMake(3, 2);
// 1.7设置图片内容
[button setImage:[UIImage imageNamed:@"player_btn_pause_normal"] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:@"player_btn_pause_highlight"] forState:UIControlStateHighlighted];
// 1.8改变图标背景色
// button.imageView.backgroundColor = [UIColor purpleColor];
// 1.9设置背景图片
// [button setBackgroundImage:[UIImage imageNamed:@"buttongreen"] forState:UIControlStateNormal];
[button setBackgroundImage:[UIImage imageNamed:@"buttongreen_highlighted"] forState:UIControlStateHighlighted];
// 2.0添加子视图
[self.view addSubview:button];
/*
// 3.1添加监听事件
监听按钮的点击
Traget:目标(让谁做事情)
action:方法(做什么事情--》方法)
Events:事件
*/
/*
SEL sel = @selector(clickBtn:);
[button addTarget:self action:sel forControlEvents:UIControlEventTouchUpInside];
*/
// [button addTarget:self action:@selector(demo) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:@selector(demoBtn:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)demo{
NSLog(@"demo");
}
-(void)demoBtn:(UIButton *)button{
NSLog(@"%@",button);
}
- (IBAction)clickBtn:(UIButton *)sender {
//self.btn.enabled = false;
sender.enabled = NO;
}
@end