美文网首页
iOS中UIStackView的使用笔记

iOS中UIStackView的使用笔记

作者: 数字d | 来源:发表于2021-09-30 18:07 被阅读0次

最近一年左右放弃使用xib来布局页面,纯代码布局中使用masonry也基本满足开发需求,只是有些xib中很好用的一些写法,用的不太熟悉,有时候难免写了很多冗余代码
还有就是在一些复杂一点儿的布局中直还是会出现一些小瑕疵,而使用系统提供的布局方式,没有出现问题,


11

1.xib中的视图1和视图2可设置比例来做适配 这个可以用autolayout来实现两个视图宽度的比例

  NSLayoutConstraint *b_consWidth = [NSLayoutConstraint constraintWithItem:self.leftView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
     [self.stackView addConstraint:b_consWidth];

同样的效果,用masonry来实现

//    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(_infoView.mas_width).multipliedBy(0.5);
//    }];

2.设置约束优先级,来控制视图的显示和隐藏

在实际的开发中如果有view1,view2,view3线性布局并左对齐,如果数据源中view2中的数据为空,则view2是需要被从视图中移除,view3要自动向view1的位置靠拢,自适应布局
在xib中拖线实现可以先设置view1,view2,view3的常规布局,
再设置一个view2被移除之后view3和view1的靠拢的约束,在xib中这个约束设置之后,约束线会自动变成红色,表明这条约束和原有的约束是有冲突的,在这样的情况下,将最后设置的约束的优先级数值改小,默认系统的优先级是从0-1000,改成一个小于1000的值即可满足需求

使用masonry来实现就需要在view的数据进行赋值的时候再讲frame更新一下,这个就有点儿令人难受了

在接下来的一个使用场景中:


12

游戏段位,擅长位置,擅长英雄,游戏范围,可接大区的布局是根据数据来做展示和隐藏,而背景的白色区域和底部的大神协议都是根据数据来自适应变化,还会根据用户操作来做展示和隐藏的变动,目测使用UIStackView来实现能够从内到外来实现UI布局

附UIStackView简单用法

#import "ViewController.h"

#import "Masonry.h"

#import "PinkViewCtl.h"

#import "SDWebImage.h"

#import "YZNeedSubModel.h"

#import "YZMessageModel.h"

#import "MJExtension.h"



#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)

@interface ViewController ()<UIScrollViewDelegate>


@property(nonatomic,strong)UIView * bgScrollView;

@property(nonatomic,strong)UIView * yzclientZBView;

@property(nonatomic,strong)UIView * tabBgView;

@property(nonatomic,strong)PinkViewCtl * vc;

@property(nonatomic,assign)CGFloat greenHeight;
@property(nonatomic,assign)CGFloat redheight;

@property(nonatomic,strong)NSMutableArray * emojArray;

@property(nonatomic,strong)UIStackView * stackView;

@property(nonatomic,strong)UIView * infoView;
@property(nonatomic,strong)UIView * leftView;
@property(nonatomic,strong)UIView * middleView;
@property(nonatomic,strong)UIView * rightView;

@end

@implementation ViewController
-(UIView *)infoView {
    if (_infoView == nil) {
        _infoView = [[UIView alloc] init];
        _infoView.backgroundColor = [UIColor redColor];
        _infoView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _infoView;
}

-(UIView *)leftView {
    if (_leftView == nil) {
        _leftView = [[UIView alloc] init];
        _leftView.backgroundColor = [UIColor greenColor];
        _leftView.translatesAutoresizingMaskIntoConstraints = NO;

    }
    return _leftView;
}

-(UIView *)middleView {
    if (_middleView == nil) {
        _middleView = [[UIView alloc] init];
        _middleView.backgroundColor = [UIColor blueColor];
        _middleView.translatesAutoresizingMaskIntoConstraints = NO;

    }
    return _middleView;
}

-(UIView *)rightView {
    if (_rightView == nil) {
        _rightView = [[UIView alloc] init];
        _rightView.backgroundColor = [UIColor systemPinkColor];
        _rightView.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return _rightView;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.stackView = [[UIStackView alloc] init];
    [self.view addSubview:self.stackView];
//
    self.stackView.axis  = UILayoutConstraintAxisHorizontal;
    self.stackView.spacing = 0;
    self.stackView.alignment = UIStackViewAlignmentFill;
//    self.stackView.distribution = UIStackViewDistributionFillEqually;
    [self.stackView addArrangedSubview:self.infoView];
    [self.stackView addArrangedSubview:self.leftView];
    [self.stackView addArrangedSubview:self.middleView];
    [self.stackView addArrangedSubview:self.rightView];
    


    
    [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.bottom.equalTo(self.view);
    }];
    
    NSLayoutConstraint *b_consWidth = [NSLayoutConstraint constraintWithItem:self.leftView attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self.infoView attribute:NSLayoutAttributeWidth multiplier:0.5 constant:0.0];
       [self.stackView addConstraint:b_consWidth];

    
//    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(_infoView.mas_width).multipliedBy(0.5);
//    }];
    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_leftView.mas_width);
    }];
    [self.rightView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(_middleView.mas_width);
    }];
    
//    [self.leftView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(self.middleView.mas_width);
//    }];
//
//    [self.middleView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.width.equalTo(self.rightView.mas_width);
//    }];

}

始终觉得xib用着顺手,但是确实有一种感觉,使用纯代码实现能提升自己对这个布局的理解程度
-------------------国庆假期快乐~~

补充:后来发现Masonry也能设置视图的宽高比和两个视图之间的宽度比例
https://www.jianshu.com/p/893a09926b81?nomobile=yes

相关文章

  • iOS中UIStackView的使用笔记

    最近一年左右放弃使用xib来布局页面,纯代码布局中使用masonry也基本满足开发需求,只是有些xib中很好用的一...

  • iOS中UIStackView的使用

    闲言 最近在维护项目的过程中发现了之前的同事有使用UIStackView布局,觉得还挺方便的,因为之前并没有使用过...

  • iOS9新特性UIStackView

    概述 UIStackView是iOS9中新增的API,类似于Android中的线性布局。UIStackView提供...

  • UIStackView简单理解和使用

    一、UIStackView简介 UIStackView是iOS9中新增的API,类似于Android中的线性布局。...

  • iOS - UIStackView的使用

    UIStackView 在iOS9中苹果在UIKit框架中引入了一个新的视图类UIStackView。UIStac...

  • UIStackView使用

    什么是UIStackView? UIStackView是在iOS9中才出现的,它可以帮助我们布局UI控件,从而减少...

  • UIStackView详解

    1. 概念 苹果在iOS9中UIKit框架引入一个新的视图类UIStackView:UIStackView视图 管...

  • iOS UIStackView的使用

    效果图如下:

  • iOS9 UIStackView 使用

    iOS 9中增加了新的控件:UIStackView,看下图就明白作用了。 UIStackView 类提供了一个高效...

  • UIStackView+Separator 增加分割线

    IOS9.0以后新增控件UIStackView.项目中经常要用到分割线。下面方法能很方便给UIStackView中...

网友评论

      本文标题:iOS中UIStackView的使用笔记

      本文链接:https://www.haomeiwen.com/subject/mmienltx.html