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
#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
相关文章推荐
- OC-UI-003.开始简单的动画效果,2种方式
- UIView block动画
- AddComponentMenu ContextMenu ExecuteInEditMode HideInInspector NonSerialized RPC RequireComponent Se
- OC-UI-002.按钮的属性frame、bounds、center
- xib中UIView设置圆角
- (二)hue与HDFS、YARN集成配置与测试
- ActionContext、ValueStack、Stack Context之间的关系
- UIControl 纠错
- iOS UISearchController的使用
- Maya Mel GUI入门
- NSValue可以将结构转化为对象,并把它存储在集合中。
- 【译】UI设计基础(UI Design Basics)--启动与停止(Starting and Stopping)(五)
- EASYUI 表单(FORM)用法
- ios 触摸事件无法响应之UIViewController的误用
- Using 1.7 requires compiling with Android 4.4 (KitKat); currently using API 10
- Android Ui之Recycle View学习
- 一个关于NGUI的点击和按键响应的Bug
- iOS开发~UI布局(三)深入理解autolayout
- 如何让UITableview的sectionHeader随着tableview一起滑动
- iOS开发~UI布局(二)storyboard中autolayout和size class的使用详解