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

IOS布局笔记一(代码实现自己主动布局)

时间:2017-05-11 19:38:41      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:layout   sha   ace   链接   垂直   分享   upload   版权   sla   



1.将一个试图放置在其父视图的中央位置,使用限制条件。
2.创建两个限制条件:一个是将目标视图的 center.x 位置排列在其父视图的 center.x 位置,而且另外一个是将目标视图的 center.y 位置排列在其父视图的 center.y 位置。

3.首先在 WildCatViewController.h中加入一个Button
//
// WildCatViewController.h
// AutoLayoutDemo
//
// Created by wildcat on 14-4-20.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface WildCatViewController : UIViewController
@property(nonatomic,strong) UIButton*button;
@end
在.m文件里实现:

//
// WildCatViewController.m
// AutoLayoutDemo
//
// Created by wildcat on 14-4-20.
// Copyright (c) 2014年 com.wildcat. All rights reserved.
//
#import "WildCatViewController.h"@interface WildCatViewController ()
@end

@implementation WildCatViewController
@synthesize button=_button;
- (void)viewDidLoad
{
[super viewDidLoad];
_button=[UIButton buttonWithType:UIButtonTypeRoundedRect];
_button.translatesAutoresizingMaskIntoConstraints=NO;
[_button setTitle:@"WildCat" forState:UIControlStateNormal];
[self.view addSubview:_button];

UIView *superView=_button.superview;
//加入约束,使按钮在屏幕水平方向的中央
NSLayoutConstraint *centerXContraint=[NSLayoutConstraint
constraintWithItem:_button
attribute:NSLayoutAttributeCenterX
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterX
multiplier:1.0f
constant:0.0];
//加入约束。使按钮在屏幕垂直方向的中央
NSLayoutConstraint *centerYContraint=[NSLayoutConstraint
constraintWithItem:_button
attribute:NSLayoutAttributeCenterY
relatedBy:NSLayoutRelationEqual
toItem:superView
attribute:NSLayoutAttributeCenterY
multiplier:1.0f
constant:0.0];
//给button的父节点加入约束
[superView addConstraints:@[centerXContraint,centerYContraint]];

}

-(NSUInteger)supportedInterfaceOrientations{
return UIInterfaceOrientationMaskAll; //屏幕能够旋转
}

- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end

不要忘记更改设备能够旋转方向。
技术分享执行结果例如以下图:技术分享技术分享

本文转自:http://1.wildcat.sinaapp.com/?p=42

限制条件和他们要加入到的视图的关系图例如以下:

 

 

技术分享

 转载请注明:版权全部点击打开链接

接下来学什么:IOS布局笔记二( Visual Format Language 定义水平和垂直约束)



IOS布局笔记一(代码实现自己主动布局)

标签:layout   sha   ace   链接   垂直   分享   upload   版权   sla   

原文地址:http://www.cnblogs.com/jzdwajue/p/6842146.html

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