object-c 之autolayout

刚开始看的人暴躁,网上资料不好入门。

外文资料稍微好点。最后还是老实直接点问号直接问xcode。总算明白思路了。

花了半天时间大概了解了一下。

又花了一天时间熟悉下基本操作。

 

搞懂了就简单多了。
1.要注意树形结构所带来的思路,就是从大布局着手,整屏分为几大块。设计好这几大块之间关系。之后再分别设计每个大块内部之间关系。依次递归,直到放入所有元素。
(可以建立一个uiview来放入一些控件组合成某个大块)
2.所以只要会布局 同一个块内的元素关系。就可以组合成复杂的页面。
3.基本公式:item1.attribute1 = multiplier × item2.attribute2 + constant
意思就是说。item1的某个属性,比如item1的左边是 itme2的右边位置乘1再加某个值。
4.有3种代码风格可以写。写的很繁琐的是NSLayoutConstraint。这个写法。但是是自己最先学的。就这样吧。稍微写了个类封成几个固定的方法。方便书写代码。
 
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.translatesAutoresizingMaskIntoConstraints=false;
    
    UIView *buttomView=[UIView new];
    [self.view addSubview:buttomView];
    buttomView.backgroundColor=[UIColor redColor];
    [LSLayOut Left2Left:buttomView relation:@"=" anotherView:self.view multiplier:1.0f offset:20 addView:self.view];
    [LSLayOut top2top:buttomView relation:@"=" anotherView:self.view multiplier:1.0f offset:30 addView:self.view];
    [LSLayOut rightWidth:buttomView width:200];
    [LSLayOut buttomWidth:buttomView  height:142];
    
    UIView *rightButtom=[UIView new];
    [self.view addSubview:rightButtom];
    rightButtom.backgroundColor=[UIColor blackColor];
    
    [LSLayOut Left2right:rightButtom relation:@"=" anotherView:buttomView multiplier:1.0f offset:1 addView:self.view];
    [LSLayOut top2top:rightButtom relation:@"=" anotherView:self.view multiplier:1.0f offset:30 addView:self.view];
    [LSLayOut right2right:rightButtom relation:@"=" anotherView:self.view multiplier:1.0f offset:-1 addView:self.view];
    [LSLayOut buttom2buttom:rightButtom relation:@"=" anotherView:self.view multiplier:1.0f offset:-1 addView:self.view];
    
}

代码完全简单多了。看的舒服。比起昨天暴躁的心情,终于平复了。

附上自己写的简陋的语法糖类。

感觉很好懂。如

+(void)Left2Left:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
左边靠一个控件的左边: 谁, 等于还是大于,靠谁,因子,偏移量, 约束加到哪个view中。
//
//  LSLayOut.m
//  layout
//
//  Created by liangshun on 18/6/12.
//  Copyright © 2018年 liangshun. All rights reserved.
//

#import "LSLayOut.h"

@implementation LSLayOut
+(void)Left2Left:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeLeft multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}
+(void)Left2right:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeRight multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}

+(void)top2top:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeTop multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}
+(void)top2buttom:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeBottom multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}

+(void)right2right:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeRight multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}

+(void)right2left:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeLeft multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}

+(void)rightWidth:(UIView *)myview  width:(NSInteger)width
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:width];
    [myview addConstraint:constraint];
}

+(void)buttom2buttom:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeBottom multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}

+(void)buttom2top:(UIView *)myview relation:(NSString *)relation anotherView:(UIView *)anotherView multiplier:(float)multiplier offset:(NSInteger)offset  addView:(UIView *)addView;
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:anotherView attribute:NSLayoutAttributeTop multiplier:multiplier constant:offset];
    [addView addConstraint:constraint];
}

+(void)buttomWidth:(UIView *)myview  height:(NSInteger)height
{
    myview.translatesAutoresizingMaskIntoConstraints=false;
    NSLayoutConstraint *constraint=nil;
    constraint=[NSLayoutConstraint constraintWithItem:myview attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0f constant:height];
    [myview addConstraint:constraint];
}


@end

 

posted @ 2018-06-12 17:52  琴鸟  阅读(295)  评论(1编辑  收藏  举报