代码生成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
- 代码生成UIButton控件
- swift代码创建控件之UIButton
- UIButton 控件
- UIButton控件
- 代码动态生成控件
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- UIKit---UIButton状态,平移,缩放,代码方式动态添加控件
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- IOS代码实现常用控件UIButton、UISlider、UISwitch、UISegmentedControl
- C#生成HTML控件 代码
- java代码动态生成控件
- UIButton按钮控件
- UIButton按钮控件
- IOS 控件 UIButton button
- UIButton按钮控件
- UIButton按钮控件
- UIButton 按钮控件
- java基础算法学习之概率算法
- yunos的工程模式
- select系统调用
- 从追MM谈Java的23种设计模式(转
- Java学习-14天
- 代码生成UIButton控件
- 未来属于SVO? - KlayGE游戏引擎
- Java学习-15天
- UIViewController原理详解
- 求解网络最大流问题
- valse2016参会总结
- 将二进制数据嵌入 XML 文档的三种方法
- 设计模式读书笔记之面向对象的几大原则
- static关键字的作用