代码实现控件:
//实例一个窗口对象
self.window=[[UIWindow alloc]init];
//自己建的视图控制器
MainViewController * mvc=[[MainViewController alloc]init];
//将自己建的视图控制器添加到系统视图
self.window.rootViewController=mvc;
// 设置背景颜色
mvc.view.backgroundColor=[UIColor grayColor];
// 设置为可见属性
[self.window makeKeyAndVisible];
用xib实现控件:
self.window = [[UIWindow alloc]init];
Main1ViewController * mvc1=[[Main1ViewController alloc]initWithNibName:@"Main1ViewController" bundle:nil];
self.window.rootViewController=mvc1;
[self.window makeKeyAndVisible];
//插座变量:类中的实例变量和xib中控件进行连接 IBOutlet
//目标动作:类中的方法和xib中的控件进行连接 IBAction
@property(strong) IBOutlet UITextField *password;
- (IBAction)Click:(id)sender;
控件的各种属性
//当视图出现自动调用viewDidLoad方法
- (void)viewDidLoad {
[super viewDidLoad];
//1、初始化
//2、设置控件大小和位置
//3、设置属性
//4、添加到视图中
//标题
//初始化
title = [[UILabel alloc] init];
//设置大小和位置
title.frame = CGRectMake(20, 20, 200, 40);
//属性设置
[title setText:@"QQ登录"];
title.font = [UIFont systemFontOfSize:15.0f];
[title setTextAlignment:NSTextAlignmentCenter];
[title setTextColor:[UIColor redColor]];
//添加到视图上
[self.view addSubview:title];
//获取屏幕大小
NSLog(@"%f",self.view.frame.size.width); //宽
NSLog(@"%f",self.view.frame.size.height);//高
//用户名输入框
userField = [[UITextField alloc] init];
[userField setFrame:CGRectMake(20, 70, 200, 35)];
//设置提示语
[userField setPlaceholder:@"请输入用户名"];
//设置输入框最后的清除按钮
[userField setClearButtonMode:UITextFieldViewModeWhileEditing];
//设置键盘的类型
[userField setKeyboardType:UIKeyboardTypeURL];
//设置键盘的返回按钮
[userField setReturnKeyType:UIReturnKeyGo];
[userField setBorderStyle:UITextBorderStyleLine];
[userField setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:userField];
//按钮
UIButton *loginBtn = [[UIButton alloc] init];
//UIButton *loginBtn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
loginBtn.frame = CGRectMake(20, 115, 80, 40);
[loginBtn setTitle:@"登录" forState:UIControlStateNormal];
[loginBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[loginBtn setTitle:@"你点我" forState:UIControlStateHighlighted];
//设置背景颜色
[loginBtn setBackgroundColor:[UIColor grayColor]];
//设置圆角
loginBtn.layer.cornerRadius = 20;
loginBtn.layer.borderWidth = 5;
//按钮添加响应事件(按钮按下触发事件)
[loginBtn addTarget:self action:@selector(ToLogin) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:loginBtn];