代码生成UIButton控件

在storyboard中可以直接拖拽控件,进行该控件的相关设置,但是也是可以在代码中生成相关的控件,只不过稍微有些麻烦,拖拽控件加快了开发的进度,可以腾出时间来放在业务逻辑的处理上来。下面直接上代码。

//
//  ViewController.m
//  CodeForButton
//
//
//  Copyright © 2016年 Lix Li. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    UIButton  *button=[UIButton buttonWithType:UIButtonTypeCustom]; //自定义的button附带button的类型
    
    [button setFrame:CGRectMake(100, 200, 100,100)]; //设置控件的位置尺寸,否则不会显示
    
    [button setTitle:@"点击我" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [button  setTitle:@"红脸" forState:UIControlStateHighlighted];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    
    [button setBackgroundImage:[UIImage imageNamed:@"writeface.png"] forState:UIControlStateNormal]; //设置背景图片(图片放在资产目录下)
    [button setBackgroundImage:[UIImage imageNamed:@"hongface.png"] forState:UIControlStateHighlighted];//高亮状态下的图片设置
    
    [button addTarget:self action:@selector(buttonClicked) forControlEvents:UIControlEventTouchUpInside]; //设置点击事件
    
    [self.view addSubview:button]; //在视图控制器的view中添加子view
    
    
    
}

-(void)buttonClicked{
    //    NSLog(@"click");
    //    UIAlertController *alert=[UIAlertController alertControllerWithTitle:@"提示" message:@"button clicked" preferredStyle:UIAlertControllerStyleAlert];
    //
    //    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
    //                                                          handler:^(UIAlertAction * action) {}];
    //
    //    [alert addAction:defaultAction];
    //    [self presentViewController:alert animated:YES completion:nil];
    
    
    
    //点击后的弹出框
    UIAlertView*alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                  message:@"这是一个自定义button的点击事件!"
                                                 delegate:nil
                                        cancelButtonTitle:@"确定"
                                        otherButtonTitles:nil];
    [alert show];
    
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

   当然这是个最基本的设置,还有其他的属性没有相关设置。

    





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值