不使用storyboard(或xib),代码创建的helloworld程序

本单元教程基于 ”程序员长弓——张燕广“ 的 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];
运行结果






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值