iphone-用代码创建界面(Creating Views from Code)
2015-06-10 19:39
260 查看
转载:http://www.2cto.com/kf/201205/130494.html
首先新建一个基于视图的项目,名字为ViewTest;
在ViewTestViewController.m文件viewDidLoad方法中写上如下代码:
[cpp] //创建一个视图UIView对象,获取初始化屏幕大小,UIScreen mainScreen该设备的内部屏幕,applicationFrame应用程序的屏幕面积帧点
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//设置背景颜色:灰
view.backgroundColor = [UIColor lightGrayColor];
//创建标签
CGRect frame = CGRectMake(10, 15, 300, 20);//设置标签的位置及大小x y width height表示左上角和大小
UILabel *label = [[UILabel alloc] initWithFrame:frame]; //创建并初始化标签,并且使大小是frame
label.textAlignment = UITextAlignmentCenter; //标签中文字的对齐方向是居中
label.backgroundColor = [UIColor clearColor]; //标签背景颜色透明的
label.font = [UIFont fontWithName:@"Verdana" size:20]; //标签文字的Verdana体20号大小
label.text = @"This is a label";//标签文字
label.tag = 1000;
//创建text
frame = CGRectMake(10, 70, 300, 30);
UITextField *text=[[UITextField alloc]initWithFrame:frame];
text.backgroundColor=[UIColor whiteColor];
text.text=@"我是任海丽";
//创建按钮
frame = CGRectMake(10, 120, 300, 50);//按钮位置大小
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:) //响应事件
forControlEvents:UIControlEventTouchUpInside];
//把标签,文本和按钮添加到view视图里面
[view addSubview:label];
[view addSubview:button];
[view addSubview:text];
self.view = view;
//创建一个视图UIView对象,获取初始化屏幕大小,UIScreen mainScreen该设备的内部屏幕,applicationFrame应用程序的屏幕面积帧点
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//设置背景颜色:灰
view.backgroundColor = [UIColor lightGrayColor];
//创建标签
CGRect frame = CGRectMake(10, 15, 300, 20);//设置标签的位置及大小x y width height表示左上角和大小
UILabel *label = [[UILabel alloc] initWithFrame:frame]; //创建并初始化标签,并且使大小是frame
label.textAlignment = UITextAlignmentCenter; //标签中文字的对齐方向是居中
label.backgroundColor = [UIColor clearColor]; //标签背景颜色透明的
label.font = [UIFont fontWithName:@"Verdana" size:20]; //标签文字的Verdana体20号大小
label.text = @"This is a label";//标签文字
label.tag = 1000;
//创建text
frame = CGRectMake(10, 70, 300, 30);
UITextField *text=[[UITextField alloc]initWithFrame:frame];
text.backgroundColor=[UIColor whiteColor];
text.text=@"我是任海丽";
//创建按钮 www.2cto.com
frame = CGRectMake(10, 120, 300, 50);//按钮位置大小
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:) //响应事件
forControlEvents:UIControlEventTouchUpInside];
//把标签,文本和按钮添加到view视图里面
[view addSubview:label];
[view addSubview:button];
[view addSubview:text];
self.view = view;
事件响应方法弹出一个警告框:
[html]-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
给出效果图:
什么视图界面,你都可以用代码写出来,并设置属性,当然这个不是简便的方法,没有直接拖来的方便,不过这样做是为了熟悉代码并知道是怎么实现的;每个控件都有好多属性,并且要明白是怎么用的。学习过程 中不怕麻烦,希望跟大家一块努力学习!有什么不好的地方,请多指出!!!
首先新建一个基于视图的项目,名字为ViewTest;
在ViewTestViewController.m文件viewDidLoad方法中写上如下代码:
[cpp] //创建一个视图UIView对象,获取初始化屏幕大小,UIScreen mainScreen该设备的内部屏幕,applicationFrame应用程序的屏幕面积帧点
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//设置背景颜色:灰
view.backgroundColor = [UIColor lightGrayColor];
//创建标签
CGRect frame = CGRectMake(10, 15, 300, 20);//设置标签的位置及大小x y width height表示左上角和大小
UILabel *label = [[UILabel alloc] initWithFrame:frame]; //创建并初始化标签,并且使大小是frame
label.textAlignment = UITextAlignmentCenter; //标签中文字的对齐方向是居中
label.backgroundColor = [UIColor clearColor]; //标签背景颜色透明的
label.font = [UIFont fontWithName:@"Verdana" size:20]; //标签文字的Verdana体20号大小
label.text = @"This is a label";//标签文字
label.tag = 1000;
//创建text
frame = CGRectMake(10, 70, 300, 30);
UITextField *text=[[UITextField alloc]initWithFrame:frame];
text.backgroundColor=[UIColor whiteColor];
text.text=@"我是任海丽";
//创建按钮
frame = CGRectMake(10, 120, 300, 50);//按钮位置大小
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:) //响应事件
forControlEvents:UIControlEventTouchUpInside];
//把标签,文本和按钮添加到view视图里面
[view addSubview:label];
[view addSubview:button];
[view addSubview:text];
self.view = view;
//创建一个视图UIView对象,获取初始化屏幕大小,UIScreen mainScreen该设备的内部屏幕,applicationFrame应用程序的屏幕面积帧点
UIView *view =
[[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
//设置背景颜色:灰
view.backgroundColor = [UIColor lightGrayColor];
//创建标签
CGRect frame = CGRectMake(10, 15, 300, 20);//设置标签的位置及大小x y width height表示左上角和大小
UILabel *label = [[UILabel alloc] initWithFrame:frame]; //创建并初始化标签,并且使大小是frame
label.textAlignment = UITextAlignmentCenter; //标签中文字的对齐方向是居中
label.backgroundColor = [UIColor clearColor]; //标签背景颜色透明的
label.font = [UIFont fontWithName:@"Verdana" size:20]; //标签文字的Verdana体20号大小
label.text = @"This is a label";//标签文字
label.tag = 1000;
//创建text
frame = CGRectMake(10, 70, 300, 30);
UITextField *text=[[UITextField alloc]initWithFrame:frame];
text.backgroundColor=[UIColor whiteColor];
text.text=@"我是任海丽";
//创建按钮 www.2cto.com
frame = CGRectMake(10, 120, 300, 50);//按钮位置大小
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = frame;
[button setTitle:@"Click Me, Please!" forState:UIControlStateNormal];
button.backgroundColor = [UIColor clearColor];
button.tag = 2000;
[button addTarget:self
action:@selector(buttonClicked:) //响应事件
forControlEvents:UIControlEventTouchUpInside];
//把标签,文本和按钮添加到view视图里面
[view addSubview:label];
[view addSubview:button];
[view addSubview:text];
self.view = view;
事件响应方法弹出一个警告框:
[html]-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
-(IBAction) buttonClicked: (id) sender{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Action invoked!" message:@"Button clicked!"
delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
}
给出效果图:
什么视图界面,你都可以用代码写出来,并设置属性,当然这个不是简便的方法,没有直接拖来的方便,不过这样做是为了熟悉代码并知道是怎么实现的;每个控件都有好多属性,并且要明白是怎么用的。学习过程 中不怕麻烦,希望跟大家一块努力学习!有什么不好的地方,请多指出!!!
相关文章推荐
- googel 图片搜索原理
- Algorithms—17.Letter Combinations of a Phone Number
- php调试
- 为什么 Lua 的新版本越来越慢?
- C++按指定模式截取字符串函数
- LeetCode 之 Set Matrix Zeroes — C++实现
- Go语言Slice去重
- Java知多少(88)列表和组合框
- java中sax对xml的操作
- ZH奶酪:PHP上传图片三个步骤
- java dom 对xml的操作
- 读书笔记:机器学习实战(2)——章3的决策树代码和个人理解与注释
- Tick and Tick------HDOJ杭州电(无法解释,直接看代码)
- c++ ifsteam 多次读取文件的问题
- Kmeans聚类算法及其java实现
- Java 递归算法
- 360技术开发实习笔试题1-java
- java web学习路线2
- python爬虫解决百度贴吧登陆验证码问题
- 229_尚学堂_高淇_java300集最全视频教程_【GOF23设计模式】_简单工厂模式详解