代码生成UIButton控件

来源:互联网 发布:高洛峰php视频教程 编辑:程序博客网 时间:2024/06/01 08:16

在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
原创粉丝点击