美文网首页
UIStackView使用(二)

UIStackView使用(二)

作者: ldhonline | 来源:发表于2018-10-14 16:49 被阅读0次
S1)%MG92$LMXJL1@G@{5GOU.png

LabelButton等控件一样,Stack View 也有自己的Intrinsic Size,在未设定约束时亦能确定自己的大小。其依赖于内部subView的大小和space。在subView约束未定时其本身大小同依据于本身Intrinsic Size确定。这是一个嵌套递归的过程。

做为 UIView ,它没有自己的Intrinsic Size, 但可以使用约束来产生, make.width.height.mas_equalTo(100);
注意不能设置位置相关约束,否则会变成绝对定位而失去栈布局流的特性。

UIStackView 刚好相反,如果只设置位置约束,它就是弹性盒子,Intrinsic Size 由内容决定,一旦设置了大小,就变成了刚体盒子,内容的大小与位置由盒子大小来决定。

  • 设置 UILayoutConstraintAxisHorizontal 相当于 子元素都成为 row
  • 设置 UILayoutConstraintAxisVertical 相当于子元素都成为 col

使用 UIStackView 嵌套布局cell

//
//  ESSNSTableViewCell.m
//  stackView
//
//  Created by ldhonline on 2018/10/14.
//  Copyright © 2018年 aidoutu.com. All rights reserved.
//

#import "ESSNSTableViewCell.h"
#import "masonry.h"

@implementation ESSNSTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style
                reuseIdentifier:reuseIdentifier];
    if(self){
        [self es_addSubviews];
        self.selectionStyle = UITableViewCellEditingStyleNone;
        self.translatesAutoresizingMaskIntoConstraints = NO;
    }
    return self;
}

- (void)es_addSubviews{
    
    UIStackView *main = [self rowsWithMargin:UIEdgeInsetsMake(15, 15, 15, 15) spacing:10];
    [self.contentView addSubview:main];
    [main mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(0);
        make.top.offset(0);
        make.right.offset(0);
        make.bottom.offset(0);
    }];

    UIStackView *headBar = [self colsWithMargin:UIEdgeInsetsMake(0, 0, 0, 0) spacing:10];
    [main addArrangedSubview:headBar];
    
    UIView *icon = [self box];
    icon.layer.cornerRadius = 18;
    
    [icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(36);
    }];
    
    [headBar addArrangedSubview:icon];
    
    UIStackView *userInfo = [self rowsWithMargin:UIEdgeInsetsMake(0, 0, 0, 0) spacing:5];
    [headBar addArrangedSubview:userInfo];
    
    UILabel *label = [[UILabel alloc] init];
    label.font = [UIFont systemFontOfSize:17];
    label.text = @"用户小宝";
    label.textColor = [UIColor blueColor];
    label.textAlignment = NSTextAlignmentCenter;
    [userInfo addArrangedSubview:label];
    
    UILabel *label2 = [[UILabel alloc] init];
    label2.font = [UIFont systemFontOfSize:13];
    label2.text = @"12:04 北京";
    label2.textColor = [UIColor colorWithWhite:0.7 alpha:1];
    label2.textAlignment = NSTextAlignmentCenter;
    [userInfo addArrangedSubview:label2];
    
    UILabel *label3 = [[UILabel alloc] init];
    label3.font = [UIFont systemFontOfSize:18];
    label3.text = @"之前来中国席卷票房的《碟中谍6》,让56岁依然亲身上阵各种高危动作戏的老帅哥汤姆?克鲁斯又火了一把。年过半百,汤帅依然身材干练,飞车、开直升机丝毫不怵。";
    label3.textColor = [UIColor colorWithWhite:0.1 alpha:1];
    label3.numberOfLines = 0;
    [main addArrangedSubview:label3];
    
    UIStackView *imagebox = [self colsWithMargin:UIEdgeInsetsMake(0, 0, 0, 0) spacing:5];

    int cout = ceil(rand()%3) + 1;
    
    CGFloat w = floor(([UIScreen mainScreen].bounds.size.width - 30 - 10)/3);
    
    for (int i = 0; i<cout; i++) {
        
        UIView *img = [self box];
        
        [img mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(w);
        }];
        
        [imagebox addArrangedSubview:img];
    }

    [main addArrangedSubview:imagebox];
    
}

- (UIColor *)rndColor{
    return [UIColor colorWithRed:rand()%256/255.0 green:rand()%256/255.0 blue:rand()%256/255.0 alpha:1];
}

- (UIStackView *)divWithAxis:(UILayoutConstraintAxis)layoutConstraintAxis margin:(UIEdgeInsets)marin spacing:(NSUInteger)spacing{
    UIStackView *div = [[UIStackView alloc] init];
    div.axis = layoutConstraintAxis;
    div.spacing = spacing;
    div.alignment = UIStackViewAlignmentLeading;
    div.distribution = UIStackViewDistributionEqualSpacing;
    div.layoutMargins = marin;
    div.layoutMarginsRelativeArrangement = YES;
    return div;
}

- (UIStackView *)rowsWithMargin:(UIEdgeInsets)marin spacing:(NSUInteger)spacing{
    return [self divWithAxis:UILayoutConstraintAxisVertical margin:marin spacing:spacing];
}


- (UIStackView *)colsWithMargin:(UIEdgeInsets)marin spacing:(NSUInteger)spacing{
    return [self divWithAxis:UILayoutConstraintAxisHorizontal margin:marin spacing:spacing];
}

- (UIView *)box{
    UIView * view = [[UIView alloc] init];

    view.backgroundColor = [self rndColor];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideView:)];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:tap];
    return view;
}

- (void)hideView:(UIGestureRecognizer *)gest{
    UIView *view = gest.view;
    view.hidden = YES;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    [self activeBackgroundWithIsNeed:selected];
}

-(void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated{
    [super setHighlighted:highlighted
                 animated:animated];
    [self activeBackgroundWithIsNeed:highlighted];
}

-(void)activeBackgroundWithIsNeed:(BOOL)need{
    [UIView animateWithDuration:0.3 animations:^{
        if(need){
            self.contentView.backgroundColor = [UIColor colorWithWhite:0.9 alpha:1];
        }else{
            self.contentView.backgroundColor = [UIColor whiteColor];
        }
    }];
}

@end

相关文章

  • UIStackView使用(二)

    与Label、Button等控件一样,Stack View 也有自己的Intrinsic Size,在未设定约束时...

  • UIStackView 使用

    UIStackView 只支持iOS9之后的版本,然而现在应用一般支持到iOS7,FDStackView,It w...

  • UIStackView使用

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

  • UIStackView使用

    UIStackView的使用 隐藏状态栏,设置 View controller-based status bar ...

  • UIStackView 使用

    为什么使用它 1.动态实现视图的添加。不用修改删除、添加、隐藏后的布局约束。2.布局速度快, 不需要写autola...

  • UIStackView的使用

    终于熬到了iOS9.0,如果你们公司最低支持的是iOS9的话。那么UIStackView就可以使用了。 由于UIS...

  • UIStackView的使用

    一、了解 instric content size instric content size 表示一个 view ...

  • UIStackView使用介绍

    在iOS开发中,对于控件布局我们一般是使用AutoLayout加约束的机制实现,UIKit有一个布局组件UISta...

  • Imtermediate UIStackView(二)

    Getting started 这篇的内容是接着上一篇,上篇中得4个问题,我们还没解决完。接下来就解决上次的没有解...

  • 【UIKit】UIStackView

    UIStackView UIStackView : UIViewiOS 9.0 基于 Flexbox 思想的布局方...

网友评论

      本文标题:UIStackView使用(二)

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