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

OC-UI-004.纯代码编写按钮的简单方法

2015-12-22 11:08 459 查看
通过加载view的时候写入代码,进行控件编写

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
////view加载完毕就会调用这个方法
- (void)viewDidLoad {
[super viewDidLoad]; //必须要让父类调用这个方法
UIButton *button = [[UIButton alloc] init];//创建一个按钮
UIImage *image1 = [UIImage imageNamed:@"btn_01"];//创建一张图片,PNG格式不用加后缀
[button setBackgroundImage:image1 forState:UIControlStateNormal];//设置按钮背景图
UIImage *image2 = [UIImage imageNamed:@"btn_02"];//创建第二张图
//设置背景高亮图--按钮点击下去
[button setBackgroundImage:image2 forState:UIControlStateHighlighted];
[button setTitle:@"点我呀" forState:UIControlStateNormal];//设置文字
//设置文字颜色
[button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[button setTitle:@"你妹的" forState:UIControlStateHighlighted];//设置高亮文字
//设置高亮文字颜色
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
button.frame = CGRectMake(50, 50, 100, 100);//设置位置与尺寸
[self.view addSubview:button];//在父view 添加 按钮控件
//添加事件的监听
[button addTarget:self action:@selector(show) forControlEvents:UIControlEventTouchUpInside];

UIButton *buttonPlus = [UIButton buttonWithType:UIButtonTypeContactAdd];
buttonPlus.center = CGPointMake(100, 300);
[self.view addSubview:buttonPlus];
[buttonPlus addTarget:self action:@selector(move) forControlEvents:UIControlEventTouchUpInside];
}

-(void)show{
NSLog(@"老婆真好");
}
-(void)move{
NSLog(@"老公真好");
}
//- (void)didReceiveMemoryWarning {
// [super didReceiveMemoryWarning];
// // Dispose of any resources that can be recreated.
//}

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