iOS精选从0到1学习iOS

iOS UIStackView 使用技巧:添加弹簧和固定长度

2020-08-18  本文已影响0人  一粒咸瓜子

UIStackView 类提供了一个高效的接口用于平铺一行或一列的视图组合。Stack视图管理着所有在它的 arrangedSubviews 属性中的视图的布局。这些视图根据它们在 arrangedSubviews 数组中的顺序沿着 Stack 视图的轴向排列。
简而言之,即UIStackView,就是一个ContainerView,可以沿横向或纵向按照一定的规则布局内部的子View。

1、四大属性

UIStackView主要包括了四大属性:axis、alignment、distribution、spacing。


UILayoutConstraintAxisHorizontal = 0,//水平
UILayoutConstraintAxisVertical = 1//垂直
UIStackViewAlignmentFill,//子视图填充StackView
UIStackViewAlignmentLeading,//子视图左对齐(axis为垂直方向而言)
UIStackViewAlignmentTop = UIStackViewAlignmentLeading,//子视图顶部对齐(axis为水平方向而言)
UIStackViewAlignmentFirstBaseline, // 按照第一个子视图的文字的第一行对齐,同时保证高度最大的子视图底部对齐(只在axis为水平方向有效)
UIStackViewAlignmentCenter,//子视图居中对齐
UIStackViewAlignmentTrailing,//子视图右对齐(axis为垂直方向而言)
UIStackViewAlignmentBottom = UIStackViewAlignmentTrailing,//子视图底部对齐(axis为水平方向而言)
UIStackViewAlignmentLastBaseline, // 按照最后一个子视图的文字的最后一行对齐,同时保证高度最大的子视图顶部对齐(只在axis为水平方向有效)
UIStackViewDistributionFill = 0, 填满,不符合则按优先级压缩或拉伸控件
UIStackViewDistributionFillEqually, 子控件等宽(高)
UIStackViewDistributionFillProportionally, 根据每个子视图里面内容的尺寸来进行填充操作
UIStackViewDistributionEqualSpacing, 保证间距相等
UIStackViewDistributionEqualCentering, 保证每个子视图中心直接的间距相等

2、distribution 各类模式

3、使用技巧

3.1> 善用嵌套

只要嵌套好UIStackView,就可以用很少的约束达到自动布局界面的目的。

3.2> 增量排版

如果排版比较复杂,不建议使用系统的 spacing 属性,可以生成透明视图来占位,约束视图大小就可以达到控制子视图间距的效果。

// 占位视图宏定义
#define StackFixed(w, h) \
^{\
    UIView *view = UIView.new;\
    [view mas_makeConstraints:^(MASConstraintMaker *make) {\
        make.width.mas_equalTo(w);\
        make.height.mas_equalTo(h);\
    }];\
    return view;\
}()

// 弹簧宏定义
#define StackSpring(axis) \
^{\
    UIView *view = UIView.new; \
    [view mas_makeConstraints:^(MASConstraintMaker *make) {\
        if (axis == UILayoutConstraintAxisHorizontal) {\
            make.width.mas_equalTo(1000).priorityLow();\
        } else {\
            make.height.mas_equalTo(1000).priorityLow();\
        }\
    }];\
    return view;\
}()

Demo地址StackViewDemo


iOS11 新增api

可以定制各个子控件后面的间距,但使用起来需要考虑版本适配的问题。

/* Set and get custom spacing after a view. 
 
 This custom spacing takes precedence over any other value that might otherwise be used 
 for the space following the arranged subview.
 
 Defaults to UIStackViewSpacingUseDefault (Swift: UIStackView.spacingUseDefault), where 
 resolved value will match the spacing property.
 
 You may also set the custom spacing to UIStackViewSpacingUseSystem (Swift: UIStackView.spacingUseSystem),
 where the resolved value will match the system-defined value for the space to the neighboring view, 
 independent of the spacing property.
 
 Maintained when the arranged subview changes position in the stack view, but not after it
 is removed from the arrangedSubviews list.
 
 Ignored if arrangedSubview is not actually an arranged subview.
 */
- (void)setCustomSpacing:(CGFloat)spacing afterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));
- (CGFloat)customSpacingAfterView:(UIView *)arrangedSubview API_AVAILABLE(ios(11.0),tvos(11.0));
上一篇 下一篇

猜你喜欢

热点阅读