本单元教程基于 ”程序员长弓——张燕广“ 的 iphone开发教程,使用最新版的 ios9 和 xcode7 作为开发工具编译运行。链接地址 http://blog.csdn.net/m_changgong/article/details/8029211
初始化及展示UI组件,除了使用storyboard(或xib)还可以纯手工编写Code实现(这点所以GUI语言都是一样的)
1、修改ViewController.m文件,在viewDidLoad中添加
//
// ViewController.m
// HelloWorld
//
// Created by chy龙神 on 5/18/16.
// Copyright © 2016 555chy. 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.
NSLog(@"viewDidLoad");
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
label.text = @"viewDidLoad hello world";
[self.view addSubview:label];
//[label release];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
运行结果
2、添加loadView方法
-(void)loadView {
//[super loadView];
NSLog(@"loadView");
//手工初始化view
//applicationFrame is already deprecated: first deprecated in IOS9.0
//UIView *view = [[UIView alloc] initWithFrame: [UIScreen mainScreen].applicationFrame];
UIView *view = [[UIView alloc] initWithFrame: [UIScreen mainScreen].bounds];
self.view = view;
UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(0,200,200,200)];
label2.text = @"loadView hello world";
[self.view addSubview:label2];
//'release' is unavaliable: not avaliable in automatic reference counting mode, ARC forbids explict message send of 'release'
//[label2 release];
}
运行结果
我们发现是全黑的。为什么会是这样的呢?
原因是Label的字体默认是黑色的,背景默认是透明的;而UIView的背景色则默认是黑的,所以导致了全黑的界面。
3、下面我们为UIView上色
UIColor *color = [UIColor colorWithRed:1.0 green:0 blue:0 alpha:1.0];
[view setBackgroundColor: color];
运行结果