//
// viewcontroller.m
// masonry自动布局
//
// created by 王木木 on 15/11/18.
// 尊龙凯时 copyright ? 2015年 王木木. all rights reserved.
//学习自http://www.cocoachina.com/ios/20141219/10702.html
/*
@property (nonatomic, strong, readonly) masconstraint *left; 左侧
@property (nonatomic, strong, readonly) masconstraint *top; 上侧
@property (nonatomic, strong, readonly) masconstraint *right; 右侧
@property (nonatomic, strong, readonly) masconstraint *bottom; 下侧
@property (nonatomic, strong, readonly) masconstraint *leading; 首部
@property (nonatomic, strong, readonly) masconstraint *trailing; 尾部
@property (nonatomic, strong, readonly) masconstraint *width; 宽
@property (nonatomic, strong, readonly) masconstraint *height; 高
@property (nonatomic, strong, readonly) masconstraint *centerx; 横向中点
@property (nonatomic, strong, readonly) masconstraint *centery; 纵向中点
@property (nonatomic, strong, readonly) masconstraint *baseline; 文本基线
*/
/*
首先在masonry中能够添加autolayout约束有三个函数
- (nsarray *)mas_makeconstraints:(void(^)(masconstraintmaker *make))block;
- (nsarray *)mas_updateconstraints:(void(^)(masconstraintmaker *make))block;
- (nsarray *)mas_remakeconstraints:(void(^)(masconstraintmaker *make))block;
mas_makeconstraints 只负责新增约束 autolayout不能同时存在两条针对于同一对象的约束 否则会报错
mas_updateconstraints 针对上面的情况 会更新在block中出现的约束 不会导致出现两个相同约束的情况
mas_remakeconstraints 则会清除之前的所有约束 仅保留最新的约束
三种函数善加利用 就可以应对各种情况了
*/
#import "viewcontroller.h"
#import "view masadditions.h"
#import "view masshorthandadditions.h"
@interface viewcontroller ()
@end
@implementation viewcontroller
- (void)viewdidload {
[super viewdidload];
// do any additional setup after loading the view, typically from a nib.
// 设置约束前
[self view1];
}
//1. [基础] 居中显示一个view
-(void)view1{
uiview *view = [uiview new];
view.backgroundcolor = [uicolor blackcolor];
[self.view addsubview:view];
[view mas_makeconstraints:^(masconstraintmaker *make) {
make.center.equalto(self.view);
make.size.mas_equalto(cgsizemake(300, 300));
}];
}
//2让一个view略小于其superview(边距为10)
-(void)view2{
uiview *view = [uiview new];
view.backgroundcolor = [uicolor blackcolor];
//在做autolayout之前 一定要先将view添加到superview上 否则会报错
[self.view addsubview:view];
//mas_makeconstraints就是masonry的autolayout添加函数 将所需的约束添加到block中行了
[view mas_makeconstraints:^(masconstraintmaker *make) {
//将sv居中
make.center.equalto(self.view);
//将size设置成(300,300)
make.size.mas_equalto(cgsizemake(300, 300));
}];
uiview *view1 = [uiview new];
view1.backgroundcolor = [uicolor redcolor];
[view addsubview:view1];
[view1 mas_makeconstraints:^(masconstraintmaker *make) {
// make.edges.equalto(view).width.insets(uiedgeinsetsmake(10, 10, 10, 10));
/**********************************************************************/
/*
这里有意思的地方是and和with 其实这两个函数什么事情都没做
- (masconstraint *)with {
return self;
}
- (masconstraint *)and {
return self;
}
但是用在这种链式语法中 就非常的巧妙和易懂 不得不佩服作者的心思(虽然我现在基本都会省略)
*/
make.top.equalto(view.mas_top).with.offset(20);
make.left.equalto(view).with.offset(20);
make.bottom.equalto(view).with.offset(-20);
make.right.equalto(view).with.offset(-20);
/**********************************************************************/
// make.top.left.bottom.and.right.equalto(view).with.insets(uiedgeinsetsmake(30, 30, 30, 30));
/**********************************************************************上面三种效果一样*/
}];
}
//3. [初级] 让两个高度为150的view垂直居中且等宽且等间隔排列 间隔为10(自动计算其宽度)
-(void)view3{
//括号里面是数字或定值时用 mas_equalto 括号里面是量词 对象的时候用equalto
int padd = 10;
uiview *view = [uiview new];
view.backgroundcolor = [uicolor blackcolor];
uiview *view1 = [uiview new];
view1.backgroundcolor = [uicolor yellowcolor];
uiview *view2 = [uiview new];
view2.backgroundcolor = [uicolor yellowcolor];
[self.view addsubview:view];
[view addsubview:view1];
[view addsubview:view2];
[view mas_makeconstraints:^(masconstraintmaker *make) {
make.center.equalto(self.view);
make.size.mas_equalto(cgsizemake(300, 300));
}];
[view1 mas_makeconstraints:^(masconstraintmaker *make) {
make.centery.mas_equalto(view.mas_centery);
make.left.equalto(view.mas_left).with.offset(padd);
make.right.equalto(view2.mas_left).with.offset(-padd);
make.height.mas_equalto(@150);
make.width.equalto(view2);
}];
[view2 mas_makeconstraints:^(masconstraintmaker *make) {
make.centery.mas_equalto(view.mas_centery);
make.left.equalto(view1.mas_right).with.offset(padd);
make.right.equalto(view.mas_right).with.offset(-padd);
make.height.mas_equalto(@150);
make.width.mas_equalto(view1.mas_width);
}];
}
//4. [中级] 在uiscrollview顺序排列一些view并自动计算contentsize
-(void)view4{
uiview *view = [uiview new];
view.backgroundcolor = [uicolor blackcolor];
uiscrollview *scroll = [uiscrollview new];
scroll.backgroundcolor = [uicolor purplecolor];
[self.view addsubview:view];
[view addsubview:scroll];
[view mas_makeconstraints:^(masconstraintmaker *make) {
make.center.equalto(self.view);
make.size.mas_equalto(cgsizemake(300, 300));
}];
[scroll mas_makeconstraints:^(masconstraintmaker *make) {
make.edges.equalto(view).width.insets(uiedgeinsetsmake(5, 5, 5, 5));
}];
}
-(void)view5{
uitextfield *pwdtextfield = [uitextfield new];
pwdtextfield.backgroundcolor = [uicolor blackcolor];
[self.view addsubview:pwdtextfield];
uitextfield *twopwdtextfield = [uitextfield new];
twopwdtextfield.backgroundcolor = [uicolor yellowcolor];
[self.view addsubview:twopwdtextfield];
twopwdtextfield.placeholder = @"请再次输入密码";
twopwdtextfield.layer.maskstobounds = yes;
twopwdtextfield.layer.cornerradius = 5;
[pwdtextfield mas_makeconstraints:^(masconstraintmaker *make) {
make.center.equalto(self.view);
make.size.mas_equalto(cgsizemake(300, 300));
}];
[twopwdtextfield mas_makeconstraints:^(masconstraintmaker *make) {
// make.center.equalto(self.view);
make.top.equalto(pwdtextfield.mas_bottom).with.offset(20);
// make.centery.mas_equalto(pwdtextfield.mas_centery);
make.left.mas_equalto(pwdtextfield.mas_left);
make.right.mas_equalto(pwdtextfield.mas_right);
make.width.mas_equalto(pwdtextfield.mas_width);
make.height.mas_equalto(pwdtextfield.mas_height);
}];
}
- (void)didreceivememorywarning {
[super didreceivememorywarning];
// dispose of any resources that can be recreated.
}
@end
用户登录
还没有账号?立即注册
用户注册
投稿取消
| 文章分类: |
|
还能输入300字
上传中....
閙雊了沒侑