随着iOS开发发展至今,在UI制作上逐渐分化为了三种主要流派:使用代码手写UI及布局;使用单个xib文件组织viewController或者view;使用StoryBoard来通过单个或很少的几个(文件构建全部UI。
每种方式各有优缺点,根据实际的开发项目,合适的选择一种方式来实现.(详见:http://www.cocoachina.com/industry/20140102/7640.html)
1 // 2 // ViewController.m 3 // 04-代码创建按钮 4 // 5 // Created by hukezhu on 15/4/25. 6 // 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @end 14 15 @implementation ViewController 16 17 - (void)viewDidLoad { 18 [super viewDidLoad]; 19 //1,创建按钮 20 UIButton *btn = [[UIButton alloc]init]; 21 //2,添加按钮 22 [self.view addSubview:btn]; 23 //3,设置frame 24 btn.frame = CGRectMake(50, 50, 100, 100); 25 //4,设置背景图片 26 //4.1,通过文件名加载图片(凡是PNG图片,不需要加扩展名) 27 UIImage *noraml = [UIImage imageNamed:@"mingren"]; 28 //4.2设置普通状态下的背景图片 29 [btn setBackgroundImage:noraml forState:UIControlStateNormal]; 30 31 //4.3加载高亮的图片 32 UIImage *high = [UIImage imageNamed:@"chutian"]; 33 [btn setBackgroundImage:high forState:UIControlStateHighlighted]; 34 35 //5.设置文字 36 [btn setTitle:@"点我啊" forState:UIControlStateNormal]; 37 [btn setTitle:@"摸我干啥" forState:UIControlStateHighlighted]; 38 39 //6.设置文字的颜色 40 [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; 41 42 [btn setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; 43 44 //7.监听按钮点击 45 [btn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside]; 46 47 //8. 隐藏导航栏(看不清加号按钮) 48 - (BOOL)prefersStatusBarHidden{ 49 50 return YES; 51 } 52 53 -(void)btnClick{ 54 55 NSLog(@"------------------"); 56 } 57 58 @end
时间: 2024-10-14 13:53:19