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


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

    2. 加入加控制器类. 改动控制器类的viewDidLoad

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        //创建标题
        UILabel *header = [[UILabel alloc] init];
        header.text = @"欢迎来到我的世界!";
        header.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview: header];
        
        self.statusLabel = [[UILabel alloc] init];
        self.statusLabel.text = @"准备就绪!";
        [self.view addSubview: self.statusLabel];
        
        
        //加入自己主动布局约束
        UILabel *statusLabel = self.statusLabel;
        [header setTranslatesAutoresizingMaskIntoConstraints: NO];
        [statusLabel setTranslatesAutoresizingMaskIntoConstraints: NO];
        
        NSMutableArray *contraits = [NSMutableArray array];
        NSMutableDictionary *metrics = [[NSMutableDictionary alloc] initWithObjectsAndKeys:@5, @"HPadding", @5, @"VPadding", @20, @"TopMargin", nil];
        NSDictionary *views = NSDictionaryOfVariableBindings(header, statusLabel);
        [contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[header]-HPadding-|" options:0 metrics:metrics views:views]];
        [contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[statusLabel]-HPadding-|" options:0 metrics:metrics views:views]];
        [contraits arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[statusLabel]-HPadding-|" options:0 metrics:metrics views:views]];
        [contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-TopMargin-[header]-(>=0)-[statusLabel]-VPadding-|" options:0 metrics:metrics views:views]];
    
        [self.view addConstraints: contraits];
        
    }

    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. 执行程序, 得到例如以下效果

     

  • 相关阅读:
    GJGHFD的最小树 题解 [Trie树+启发式合并]
    GJGHFD的二进制数 题解 [主席树]
    《魔女之旅》——《回溯之叹》读后感
    [BZOJ3779]重组病毒 题解 [LCT+dfs序]
    [BZOJ4025]二分图 题解 [线段树分治+并查集]
    《魔女之旅》——《瓶中的幸福》读后感
    《魔女之旅》——《孤独绽放的彼岸花》读后感
    《魔女之旅》——《在融雪之前》读后感
    [BZOJ3514]Codechef MARCH14 GERALD07加强版 题解 [LCT+主席树]
    [BZOJ3700]发展城市 题解 [RMQ求LCA+分类讨论]
  • 原文地址:https://www.cnblogs.com/mthoutai/p/6806096.html
Copyright © 2020-2023  润新知