zoukankan      html  css  js  c++  java
  • iOS: 学习笔记, 添加一个带界面约束的控制器

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

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

     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 }

    3. 修改AppDelegate.m文件

    - (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;
    }

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

  • 相关阅读:
    (Relax njuptoj)1009 数的计算(DP)
    Eclipse使用技巧总结(二)
    Ibatis的分页机制的缺陷
    TFT ST7735的Netduino驱动
    超级求爱程序--为我们的程序工作找乐子
    Selenium Grid跨浏览器-兼容性测试
    PHP一般情况下生成的缩略图都比较不理想
    库目录和头文件目录中生成画图函数
    根据PHP手册什么叫作变量的变量?
    数据库的最基本的逻辑结构组成架构
  • 原文地址:https://www.cnblogs.com/yaoyu126/p/3764423.html
Copyright © 2011-2022 走看看