iOS开发技术讨论iOS开发经验总结iOS

如何搞定Autolayout,远离自动布局带给你的烦恼

2015-07-25  本文已影响3675人  Andyzhao

首先让我们了解一下什么是Autolayout

(一)下面让我们用例子来进一步了解Autolayout

示例如下:


效果图.png

代码实现Autolayout的步骤

  - (void)addConstraint:(NSLayoutConstraint *)constraint;
  - (void)addConstraints:(NSArray *)constraints;

代码实现Autolayout的注意点

view.translatesAutoresizingMaskIntoConstraints = NO;

创建约束对象的常用方法

创建约束对象的常用方法
+(id)constraintWithItem:(id)view1 attribute:(NSLayoutAttribute)attr1 relatedBy:(NSLayoutRelation)relation toItem:(id)view2 attribute:(NSLayoutAttribute)attr2 multiplier:(CGFloat)multiplier constant:(CGFloat)c;
view1 :要约束的控件
attr1 :约束的类型(做怎样的约束)
relation :与参照控件之间的关系
view2 :参照的控件
attr2 :约束的类型(做怎样的约束)
multiplier :乘数
c :常量

要用苹果的方法代码实现如下:(额,敲得我【恶心他妈给恶心开门----恶习到家了,不过不要担心请接着往下看】)


#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    // 禁止Autoresizing转为Autolayout的约束
    blueView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:blueView];
    
    
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    // 禁止Autoresizing转为Autolayout的约束
    redView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:redView];
 
    // 添加蓝色控件的约束
    NSLayoutConstraint *blueLeftLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:20];
       [self.view addConstraint:blueLeftLc];
    NSLayoutConstraint *blueBottomLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:-20];
    [self.view addConstraint:blueBottomLc];
    
    NSLayoutConstraint *buleRightLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:-20];
    [self.view addConstraint:buleRightLc];
    
    NSLayoutConstraint *blueHLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:40];
    [self.view addConstraint:blueHLc];
    
    NSLayoutConstraint *blueWidthLc = [NSLayoutConstraint constraintWithItem:blueView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:redView attribute:NSLayoutAttributeWidth multiplier:1.0 constant:0];
    [self.view addConstraint:blueWidthLc];

    /*** 添加红色控件的约束 ***/
    NSLayoutConstraint *redTopLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
    [self.view addConstraint:redTopLc];
    
    NSLayoutConstraint *redBottomLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:blueView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
    [self.view addConstraint:redBottomLc];
    
    NSLayoutConstraint *redRightLc = [NSLayoutConstraint constraintWithItem:redView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:-20];
    [self.view addConstraint:redRightLc];
}


@end

OK看到就这么一个简单的效果要这么多代码,是不是想要抓狂,不要心慌,下面我们介绍目前最流行的Autolayout第三方框架---Masonry

同样的让我们先大致了解一个Masonry的一些情况

上面的效果图用Masonry实现如下:

#import "ViewController.h"

//define this constant if you want to use Masonry without the 'mas_' prefix
#define MAS_SHORTHAND

//define this constant if you want to enable auto-boxing for default syntax
#define MAS_SHORTHAND_GLOBALS

#import "Masonry.h"
@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 创建蓝色View
    UIView *blueView = [[UIView alloc] init];
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
    
    // 创建红色View
    UIView *redView = [[UIView alloc] init];
    redView.backgroundColor = [UIColor redColor];
    [self.view addSubview:redView];
    
    // 给蓝色View添加约束
    [blueView makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.left).offset(20);
        make.bottom.equalTo(self.view.bottom).offset(-20);
        make.right.equalTo(redView.left).offset(-20);
        make.height.equalTo(40);
        make.width.equalTo(redView.width);
        
    }];
    
    // 给红色View添加约束
    [redView makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.view.right).offset(-20);
        make.top.equalTo(blueView.top);
        make.bottom.equalTo(blueView.bottom);
   
    }];
    
    // 更新约束
    [blueView updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(80);
    }];
}

@end


上面这段代码是不是让你的心情大爽了呢,激动之余细心的读者可能发现,咦#import "Masonry.h"上面怎么有两个宏呢,下面我们来了解一下这是为什么呢

首先是Masonry中的mas_equalToequalTo

#define MAS_SHORTHAND_GLOBALS
// 注意:这个宏一定要添加到#import "Masonry.h"前面

其次是Masonry中的mas_widthwidth

 #define MAS_SHORTHAND

 mas_height、mas_centerX以此类推

总之这样简单高效的方法让我们程序员用的好爽不是吗

(二)开发中我们会遇到这样的问题- 使用到约束的优先级

// 点击屏幕时会调用此方法
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //
    [self.blueView removeFromSuperview];
    // 利用2s的时间去更新约束,在修改了约束之后,只要执行下面代码,就能做动画效果,感兴趣的读者可以试一下
    [UIView animateWithDuration:2 animations:^{
      
        // 强制更新约束(让self.view以及它的所有子控件都强制更新)
        [self.view layoutIfNeeded];
    }];
   
}
约束优先级.gif

OK这样的效果怎么实现呢,给大家看两张图大家就明白约束优先级的用处了

绿色与红色的约束优先级值.png

谢谢各位耐心观看,如有问题请留言我会及时更新改正,谢谢大家,祝各位每天开心!!!

上一篇 下一篇

猜你喜欢

热点阅读