链接地址:http://www.cnblogs.com/hukezhu/p/4500206.html
随着iOS开发发展至今,在UI制作上逐渐分化为了三种主要流派:使用代码手写UI及布局;使用单个xib文件组织viewController或者view;使用StoryBoard来通过单个或很少的几个(文件构建全部UI。
每种方式各有优缺点,根据实际的开发项目,合适的选择一种方式来实现.(详见:http://www.cocoachina.com/industry/20140102/7640.html)
// // ViewController.m // 04-代码创建按钮 // // Created by hukezhu on 15/4/25. // // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; //1,创建按钮 UIButton *btn = [[UIButton alloc]init]; //2,添加按钮 [self.view addSubview:btn]; //3,设置frame btn.frame = CGRectMake(50, 50, 100, 100); //4,设置背景图片 //4.1,通过文件名加载图片(凡是PNG图片,不需要加扩展名) UIImage *noraml = [UIImage imageNamed:@"mingren"]; //4.2设置普通状态下的背景图片 [btn setBackgroundImage:noraml forState:UIControlStateNormal]; //4.3加载高亮的图片 UIImage *high = [UIImage imageNamed:@"chutian"]; [btn setBackgroundImage:high forState:UIControlStateHighlighted]; //5.设置文字 [btn setTitle:@"点我啊" forState:UIControlStateNormal]; [btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted]; //6.设置文字的颜色 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; //7.监听按钮点击 [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; //8. 隐藏导航栏(看不清加号按钮) - (BOOL)prefersStatusBarHidden{ return YES; } -(void)btnClick{ NSLog(@"------------------"); } @end
注意在viewDidLoad方法中,可以导入view