iOS开发——纯代码界面(UIViewController和文本类控件)
一、添加视图控制器(UIViewController)
创建一个ViewController类继承UIViewController
ViewController.m做如下修改
- (void)viewDidLoad {
[super viewDidLoad];
//为了方便观察,设置背景颜色为蓝色
self.view.backgroundColor = [UIColor blueColor];
}
AppDelegate.m中做如下修改,记得导入ViewController类
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
ViewController *view = [[ViewController alloc]init];
self.window.rootViewController = view;
[self.window makeKeyAndVisible];
return YES;
}
运行程序就可以看到如下的界面
二、添加UILable,UITextField,UITextView(文本类控件)
UILable,UITextField,UITextView这几个控件是在上面添加了UIController的基础上添加的,同样修改ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
//为了方便观察,设置背景颜色为蓝色
self.view.backgroundColor = [UIColor blueColor];
//设置控件lable的位置
UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(50, 50, 150, 50)];
//设置lable文本内容为I am UILable
[lable setText:@"I am UILable"];
//设置lable文本中的文字居中
/*
UITextAlignmentCenter 居中
UITextAlignmentLeft 左对齐
UITextAlignmentRight 右对齐
*/
lable.textAlignment = UITextAlignmentCenter;
//把lable控件添加到当前的View上
[self.view addSubview:lable];
//设置控件textField的位置
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(50, 100, 200, 200)];
//设置控件textField的位置
[textField setText:@"I am UITextField"];
//把textField控件添加到当前的View上
[self.view addSubview:textField];
UITextView *textView = [[UITextView alloc] initWithFrame:CGRectMake(50, 300, 200, 100)];
[textView setText:@"I am UITextView"];
[self.view addSubview:textView];
}
运行模拟器界面如下