iOS開發——純程式碼介面(UIViewController和文字類控制元件)
阿新 • • 發佈:2019-01-10
一、新增檢視控制器(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];
}
執行模擬器介面如下