码迷,mamicode.com
首页 > 其他好文 > 详细

开发进阶05_代码创建按钮和文本框

时间:2014-10-28 00:36:40      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:style   io   color   ar   for   sp   strong   文件   div   

在控制器的view加载完毕的时候会调用一次viewDidLoad方法
 
#pragma mark 控制器的view加载完毕的时候会调用一次
- (void)viewDidLoad {
    [super viewDidLoad];
   
    //1.创建按钮
    //1.1.创建
    UIButton *btn = [[UIButton alloc] init];
 
    //1.2.设置按钮的尺寸和位置
    btn.frame = CGRectMake(0, 20, 100, 100);
 
    //1.3.设置按钮普通状态下的属性
    //1.3.1.设置背景图片
 [UIImage imageNamed:@"btn_01.png"];通过这样的方式将图片文件名转换成UIImage对象 
 setBackgroundImage:设置背景图片
 UIControlStateNormal:默认情况下的按钮
 
    UIImage *normal = [UIImage imageNamed:@"btn_01.png"];
    [btn setBackgroundImage:normal forState:UIControlStateNormal];
    
    //1.3.2.设置文字
setTitle:设置按钮上显示的文本
    [btn setTitle:@"点我啊" forState:UIControlStateNormal];
 
    //1.3.3.设置文字颜色
setTitleColor:设置按钮显示文本的颜色
    [btn setTitleColor:[UIColor greenColor] forState:UIControlStateNormal];
   
    //1.4.设置按钮高亮状态下的属性
    //1.4.1.设置背景图片
forState:UIControlStateHighlighted:设置高亮状态下的按钮
    UIImage *high = [UIImage imageNamed:@"btn_02.png"];
    [btn setBackgroundImage:high forState:UIControlStateHighlighted];
 
    //1.4.2.设置文字
    [btn setTitle:@"点我啊" forState:UIControlStateHighlighted];
 
    //1.4.3.设置文字颜色
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateHighlighted];
 
    //1.5.监听按钮点击
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
 
    //2.添加按钮到控制器的view中
    [self.view addSubview:btn];
   
    //3.添加文本输入框
    UITextField *field = [[UITextField alloc] init];
    field.frame = CGRectMake(100, 100, 100, 50);
    field.backgroundColor = [UIColor greenColor];
   
    //中心点
    CGFloat centerX = self.view.frame.size.width * 0.5;
    CGFloat centerY = self.view.frame.size.height * 0.5;
    field.center = CGPointMake(centerX, centerY);
 
    //设置字体
    field.font = [UIFont systemFontOfSize:25];
   
    [self.view addSubview:field];
   
   
}
 
- (void)btnClick:(UIButton *)btn
{
    NSLog(@"-------%p",btn);
}

开发进阶05_代码创建按钮和文本框

标签:style   io   color   ar   for   sp   strong   文件   div   

原文地址:http://www.cnblogs.com/yaofch107/p/4055409.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!