iOS: 学习笔记, 添加一个带界面约束的控制器
2014-06-02 16:24
901 查看
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. 运行程序, 得到如下效果
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. 运行程序, 得到如下效果
相关文章推荐
- iOS: 学习笔记, 加入一个带界面约束的控制器
- iOS学习笔记-workspace添加工程时只有一个proj文件问题
- iOS学习笔记 4 —— 代码创建界面UIView、UILabel、UITextField、UIButton
- IOS学习笔记32—使用Storyboard实现复杂界面
- iOS学习笔记(4)@property和类型之间用括号添加一些额外的指示符的作用
- ios学习笔记----实现一个带滑动手势的tabBarViewController,并可自定义tabBar
- WPF学习——制作一个简单的录入界面(1): 添加需要的控件
- Android Fragement学习笔记(二)----Fragement界面添加和管理
- ios学习笔记block回调的应用(一个简单的例子)
- Android Fragment学习笔记(二)----Fragment界面添加和管理
- DayDayUp之HTML5学习笔记 二 使用header、aside、section、footer实现一个简单的界面
- sql语句学习笔记(8)-添加约束,实现数据完整性
- ios学习笔记(五)xcode4.3.2中添加Window-Based Application模版
- WPF学习笔记--一个具有拖拽、平移、放大等界面导航功能的窗体
- SilverLight学习笔记--实际应用(一)(2):手把手建立一个Silverlight应用程序之添加记录
- iOS学习笔记——随机打乱一个数组
- 井位坐标添加到Las文件功能——Tkinter界面开发学习笔记一
- IOS学习笔记之实现一个简单的表
- IOS学习笔记32—使用Storyboard实现复杂界面
- iOS学习笔记----实现一个带有渐变的UIView