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:@, @"HPadding", @, @"VPadding", @, @"TopMargin", nil];
NSDictionary *views = NSDictionaryOfVariableBindings(header, statusLabel);
[contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[header]-HPadding-|" options: metrics:metrics views:views]];
[contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[statusLabel]-HPadding-|" options: metrics:metrics views:views]];
[contraits arrayByAddingObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-HPadding-[statusLabel]-HPadding-|" options: metrics:metrics views:views]];
[contraits addObjectsFromArray:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-TopMargin-[header]-(>=0)-[statusLabel]-VPadding-|" options: 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. 运行程序, 得到如下效果

iOS: 学习笔记, 添加一个带界面约束的控制器的更多相关文章

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

    1. 创建一个空iOS应用程序(Empty Application). 2. 加入加控制器类. 改动控制器类的viewDidLoad - (void)viewDidLoad { [super view ...

  2. iOS学习笔记——AutoLayout的约束

    iOS学习笔记——AutoLayout约束 之前在开发iOS app时一直以为苹果的布局是绝对布局,在IB中拖拉控件运行或者直接使用代码去调整控件都会发上一些不尽人意的结果,后来发现iOS在引入了Au ...

  3. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  4. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  5. [置顶] iOS学习笔记47——图片异步加载之EGOImageLoading

    上次在<iOS学习笔记46——图片异步加载之SDWebImage>中介绍过一个开源的图片异步加载库,今天来介绍另外一个功能类似的EGOImageLoading,看名字知道,之前的一篇学习笔 ...

  6. IOS学习笔记48--一些常见的IOS知识点+面试题

      IOS学习笔记48--一些常见的IOS知识点+面试题   1.堆和栈什么区别? 答:管理方式:对于栈来讲,是由编译器自动管理,无需我们手工控制:对于堆来说,释放工作由程序员控制,容易产生memor ...

  7. iOS学习笔记-自定义过渡动画

    代码地址如下:http://www.demodashi.com/demo/11678.html 这篇笔记翻译自raywenderlick网站的过渡动画的一篇文章,原文用的swift,由于考虑到swif ...

  8. iOS学习笔记-地图MapKit入门

    代码地址如下:http://www.demodashi.com/demo/11682.html 这篇文章还是翻译自raywenderlich,用Objective-C改写了代码.没有逐字翻译,如有错漏 ...

  9. iOS学习笔记22-推送通知

    一.推送通知 推送通知就是向用户推送一条信息来通知用户某件事件,可以在应用退到后台后,或者关闭后,能够通过推送一条消息通知用户某件事情,比如版本更新等等. 推送通知的常用应用场景: 一些任务管理APP ...

随机推荐

  1. asp.net redis 实战

    当开始用    var result=new RedisClient("127.0.0.1",6379,1"1111");  这个helper ,后面有并发之后 ...

  2. 阅读STL源码剖析之list

    首先,以我之愚见,觉得有两个地方可以优化一下,不知对否,有待商榷: 1.在list的结点定义中 template<typename T> struct __list_node { type ...

  3. 显示、隐藏 PowerShell

    以下是一个快速实现该需求的 module.只要将以下代码复制粘贴到 Documents\WindowsPowerShell\Packages\PowerShell\PowerShell.psm1 即可 ...

  4. struts2自己定义拦截器

    首先是struts2框架中基本的两个文件:一个是拦截器类,另外一个是struts.xml配置文件. 简单来说,一个是说明要做的事,一个是说明在哪里做的. 拦截器类 package com.test.i ...

  5. [CSS] DOM Hierarchy Pseudo Classes :first-child :last-child :nth-child (demystified)

    DOM hierarchy pseudo-classes allow you to style specific elements based on where they fall in the hi ...

  6. Android M(6.0) 权限爬坑之旅

    坑一:用Android5.0编译的apk,在Android6.0上运行完全没有问题. 在Android6.0以上才需要在运行时请求权限,在旧Android版本上保留原有逻辑,安装时授予权限. 用旧版本 ...

  7. 空格&amp;nbsp在不同浏览器中显示距离不一致问题解决方法

      在ie.firefox.chrome浏览器上显示的效果不太一样,主要是前面的空格宽度不同. 网上资料说不同的浏览器会有不同的默认字体.一般 IE默认字体都是宋体,而firefox和chrome的默 ...

  8. Java Interview Reference Guide--reference

    Part 1 http://techmytalk.com/2014/01/24/java-interview-reference-guide-part-1/ Posted on January 24, ...

  9. Android BLE开发——Android手机与BLE终端通信初识

    蓝牙BLE官方Demo下载地址:   http://download.csdn.net/detail/lqw770737185/8116019参考博客地址:    http://www.eoeandr ...

  10. 字符串反转实现(C++)

    字符串反转 C++实现,不使用系统函数: // ReverseString.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include ...