IOS开发之PureLayout的使用

2023-11-02  本文已影响0人  小羊爱学习

PureLayout下载地址

https://github.com/PureLayout/PureLayout

PureLayout布局使用

    UIView *myview=[[UIView alloc]init];
    UILabel *label=[[UILabel alloc]init];
    UIButton *btn=[[UIButton alloc]init];
    myview.translatesAutoresizingMaskIntoConstraints=NO;
    label.translatesAutoresizingMaskIntoConstraints=NO;
    btn.translatesAutoresizingMaskIntoConstraints=NO;
    [self.view addSubview:myview];
    [self.view addSubview:label];
    [self.view addSubview:btn];

接下来我们就来看看purelayout封装了哪些好用的布局方法:

 // myview左边距离父视图左边 10 点.
    [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:10.f];
    
    // myview1顶边距离父视图顶部 10 点.
    [myview autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10.f];
    
    // myview的左边位于btn的右边 10 点的地方.
    [myview autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:btn withOffset:10.f];
    
    // myview的右边位于btn左边 -10 点的地方, 从右往左、从下往上布局时数值都是负的。
    [myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:btn withOffset:-10.f];
    
    // 根据label的固有内容尺寸设置它的尺寸
    [label autoSetDimensionsToSize:CGSizeMake(10.f, 10.f)];
    
    // myview、label、btn水平对齐。(意思就是说只需要设置其中一个的垂直约束(y)即可)(这个可以设置多个view水平对齐)
    [@[myview, label, btn] autoAlignViewsToAxis:ALAxisHorizontal];

    //myview,label水平对齐(这个是针对两个view的水平对齐)
    [myview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:label];
    
    // myview顶部距离label的底部 10 点.
    [myview autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:label withOffset:10.f];
    
    // myview左边紧贴父视图左边
    [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    
    // myview的宽度等于父视图的宽度
    [myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:self.view];
    
    // myview的宽度设置为50
    [myview autoSetDimension:ALDimensionWidth toSize:30];
    
    // myview与label左对齐
    [myview autoPinEdge:ALEdgeLeading toEdge:ALEdgeLeading ofView:label];
    
    // myview与label水平对齐
    [myview autoAlignAxis:ALAxisHorizontal toSameAxisOfView:label];
    
    //myview右边雨label左边的距离大于等于20
    [myview autoPinEdge:ALEdgeTrailing toEdge:ALEdgeLeading ofView:label withOffset:20.f relation:NSLayoutRelationGreaterThanOrEqual];

    //myview和btn同宽(同高也一样)
    [@[myview,btn] autoMatchViewsDimension:ALDimensionWidth];//这种可以设置多个view同宽
    [myview autoMatchDimension:ALDimensionWidth toDimension:ALDimensionWidth ofView:btn];//这种只能设置两个view同宽

注意:这里我主要讲一下NSLayoutAttributeLeft ,NSLayoutAttributeRight,和NSLayoutAttributeLeading,NSLayoutAttributeTrailing之间的区别。
其实在中国, NSLayoutAttributeLeft 和 NSLayoutAttributeLeading 是一个效果的,因为我们布局习惯从左到右。但在有些国家地区,NSLayoutAttributeRight和NSLayoutAttributeLeading 是一个效果,布局习惯从右往左。这点我们大可放心,建议大家还是使用NSLayoutAttributeLeading,NSLayoutAttributeTrailing来表示左和右。

PureLayout更改约束

我们点击它的方法进去先看一下:

- (NSLayoutConstraint *)autoAlignAxisToSuperviewMarginAxis:(ALAxis)axis
{
    self.translatesAutoresizingMaskIntoConstraints = NO;
    ALView *superview = self.superview;
    NSAssert(superview, @"View's superview must not be nil.\nView: %@", self);
    ALMarginAxis marginAxis = [NSLayoutConstraint al_marginAxisForAxis:axis];
    return [self autoConstrainAttribute:(ALAttribute)axis toAttribute:(ALAttribute)marginAxis ofView:superview];
}

#endif /* __PureLayout_MinBaseSDK_iOS_8_0 */


#pragma mark Pin Edges to Superview

/**
 Pins the given edge of the view to the same edge of its superview.
 
 @param edge The edge of this view and its superview to pin.
 @return The constraint added.
 */
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge
{
    return [self autoPinEdgeToSuperviewEdge:edge withInset:0.0];
}

/**
 Pins the given edge of the view to the same edge of its superview with an inset.
 
 @param edge The edge of this view and its superview to pin.
 @param inset The amount to inset this view's edge from the superview's edge.
 @return The constraint added.
 */
- (NSLayoutConstraint *)autoPinEdgeToSuperviewEdge:(ALEdge)edge withInset:(CGFloat)inset
{
    return [self autoPinEdgeToSuperviewEdge:edge withInset:inset relation:NSLayoutRelationEqual];
}

发现这些方法都会返回一个NSLayoutConstraint。那么更新就好办了

//首先定义好一个NSLayoutConstraint属性
@property (nonatomic, strong)NSLayoutConstraint *myyueshu;
//如果我们点击按钮需要更改某个view的高度从100到200
    UIView *myview = [[UIView alloc]init];
    self.myview = myview;
    [self.view addSubview:myview];
    myview.backgroundColor = [UIColor redColor];
    [self.view addSubview:myview];
    myview.translatesAutoresizingMaskIntoConstraints = NO;
    [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [myview autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    [myview autoPinEdgeToSuperviewEdge:ALEdgeTop];
//    [myview autoSetDimension:ALDimensionHeight toSize:100];
    self.myyueshu =  [myview autoSetDimension:ALDimensionHeight toSize:100];

    UIButton *btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    btn.frame = CGRectMake(10, 400, 40, 40);
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];

把设置高度从原先的100变为200,我们直接改变了它的约束数值

- (void)btnclick{
    self.myyueshu.constant = 200;
    
}

或者先把原先的那个约束移除,然后重新添加新的约束。

- (void)btnclick{
    
    [self.myyueshu autoRemove];
    self.myyueshu = [self.myview autoSetDimension:ALDimensionHeight toSize:200];
}

如果我们需要把所有约束都重写,以上代码也可以做到,只是要定义4个约束属性,然后各个移除而已。当然我们也可以用别的方法

//首先定义好一个数组NSArray
@property (nonatomic, strong) NSArray *contentConstrains;
//接着在viewDidLoad中实现以下代码:
    UIView *myview = [[UIView alloc]init];
    self.myview = myview;
    [self.view addSubview:myview];
    myview.backgroundColor = [UIColor redColor];
    [self.view addSubview:myview];
    myview.translatesAutoresizingMaskIntoConstraints = NO;
    NSLayoutConstraint *s1 = [myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    NSLayoutConstraint *s2 = [myview autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    NSLayoutConstraint *s3 = [myview autoPinEdgeToSuperviewEdge:ALEdgeTop];
    NSLayoutConstraint *s4 = [myview autoSetDimension:ALDimensionHeight toSize:100];
    self.contentConstrains = [NSArray arrayWithObjects:s1,s2,s3,s4, nil];
    
    UIButton *btn = [[UIButton alloc]init];
    [self.view addSubview:btn];
    btn.frame = CGRectMake(10, 400, 40, 40);
    btn.backgroundColor = [UIColor redColor];
    [btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];

这样也可以实现整体更改约束。

- (void)btnclick{
    
    if(self.contentConstrains != nil){
        [self.contentConstrains autoRemoveConstraints];
    }
    
    [self.myview autoPinEdgeToSuperviewEdge:ALEdgeLeading];
    [self.myview autoPinEdgeToSuperviewEdge:ALEdgeTrailing];
    [self.myview autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:30];
    [self.myview autoSetDimension:ALDimensionHeight toSize:200];
   
}
上一篇 下一篇

猜你喜欢

热点阅读