码迷,mamicode.com
首页 > 移动开发 > 详细

iOS: 学习笔记, 添加一个带界面约束的控制器

时间:2014-06-02 18:27:57      阅读:347      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

1. 创建一个空iOS应用程序(Empty Application).

2. 添加加控制器类. 修改控制器类的viewDidLoad

bubuko.com,布布扣
 1 - (void)viewDidLoad
 2 {
 3     [super viewDidLoad];
 4     //创建标题
 5     UILabel *header = [[UILabel alloc] init];
 6     header.text = @"欢迎来到我的世界!";
 7     header.textAlignment = NSTextAlignmentCenter;
 8     [self.view addSubview: header];
 9     
10     self.statusLabel = [[UILabel alloc] init];
11     self.statusLabel.text = @"准备就绪!";
12     [self.view addSubview: self.statusLabel];
13     
14     
15     //添加自动布局约束
16     UILabel *statusLabel = self.statusLabel;
17     [header setTranslatesAutoresizingMaskIntoConstraints: NO];
18     [statusLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
19     
20     NSMutableArray *contraits = [NSMutableArray array];
21     NSMutableDictionary *metrics = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@5, @"HPadding", @5, @"VPadding", @20, @"TopMargin", nil];
22     NSDictionary *views = NSDictionaryOfVariableBindings(header, statusLabel);
23     [contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[header]-HPadding-|" options:0 metrics:metrics views:views]];
24     [contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[statusLabel]-HPadding-|" options:0 metrics:metrics views:views]];
25     [contraits arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[statusLabel]-HPadding-|" options:0 metrics:metrics views:views]];
26     [contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-TopMargin-[header]-(>=0)-[statusLabel]-VPadding-|" options:0 metrics:metrics views:views]];
27 
28     [self.view addConstraints: contraits];
29     
30 }
bubuko.com,布布扣

3. 修改AppDelegate.m文件

bubuko.com,布布扣
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    
    //加入控制器
    self.window.rootViewController = [[DemoViewController alloc] initWithNibName:nil bundle:nil];
    
    [self.window makeKeyAndVisible];
    return YES;
}
bubuko.com,布布扣

4. 运行程序, 得到如下效果

bubuko.com,布布扣

 

iOS: 学习笔记, 添加一个带界面约束的控制器,布布扣,bubuko.com

iOS: 学习笔记, 添加一个带界面约束的控制器

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/yaoyu126/p/3764423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!