gsdios / SDAutoLayout

One line of code to implement automatic layout. 一行代码搞定自动布局!支持Cell和Tableview高度自适应,Label和ScrollView内容自适应,致力于做最简单易用的AutoLayout库。The most easy way for autoLayout. Based on runtime.
MIT License
5.9k stars 1.28k forks source link

对demoVC2.m 内 ----- 设置一排固定间距自动宽度子view----- 这就里面的实现有问题把? #180

Closed DolphinsLoveCats closed 7 years ago

DolphinsLoveCats commented 7 years ago

您好,在次打扰 我对与demoVC2.m中 此方法有疑惑

// 设置一排固定间距自动宽度子view
[self setupAutoWidthViewsWithCount:4 margin:10];

进入方法

// 设置一排固定间距自动宽度子view
- (void)setupAutoWidthViewsWithCount:(NSInteger)count margin:(CGFloat)margin
{
    _autoWidthViewsContainer = [UIView new];
    _autoWidthViewsContainer.backgroundColor = [UIColor greenColor];
    [self.view addSubview:_autoWidthViewsContainer];

    NSMutableArray *temp = [NSMutableArray new];
    for (int i = 0; i < count; i++) {
        UIView *view = [UIView new];
        view.backgroundColor = [UIColor orangeColor];
        [_autoWidthViewsContainer addSubview:view];
        view.sd_layout.autoHeightRatio(0.4); // 设置高度约束
        [temp addObject:view];
    }

    _autoWidthViewsContainer.sd_layout
    .leftSpaceToView(self.view, 10)
    .rightSpaceToView(self.view, 10)
    .topSpaceToView(_centerButton, 10);

    // 此步设置之后_autoWidthViewsContainer的高度可以根据子view自适应
    [_autoWidthViewsContainer setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:4 verticalMargin:margin horizontalMargin:margin verticalEdgeInset:5 horizontalEdgeInset:10];

}

此段代码中最后一段

// 此步设置之后_autoWidthViewsContainer的高度可以根据子view自适应
[_autoWidthViewsContainer setupAutoWidthFlowItems:[temp copy] withPerRowItemsCount:4 verticalMargin:margin horizontalMargin:margin verticalEdgeInset:5 horizontalEdgeInset:10];

我进入看具体的实现方法,发现 这个高度的取值仅仅是取到的数组内最后一个view的,[self setupAutoHeightWithBottomView:viewsArray.lastObject bottomMargin:vInset];但是在此 _autoWidthViewsContainerview的高度如果都不固定,那么是不是应该取数组内最高的一个呢?而不是最后的一个,方法如下:UIView+SDAutoLayout.m 内.

- (void)setupAutoWidthFlowItems:(NSArray *)viewsArray withPerRowItemsCount:(NSInteger)perRowItemsCount verticalMargin:(CGFloat)verticalMargin horizontalMargin:(CGFloat)horizontalMagin verticalEdgeInset:(CGFloat)vInset horizontalEdgeInset:(CGFloat)hInset
{
    self.sd_categoryManager.flowItems = viewsArray;
    self.sd_categoryManager.perRowItemsCount = perRowItemsCount;
    self.sd_categoryManager.verticalMargin = verticalMargin;
    self.sd_categoryManager.horizontalMargin = horizontalMagin;
    self.verticalEdgeInset = vInset;
    self.horizontalEdgeInset = hInset;

    self.sd_categoryManager.lastWidth = 0;

    if (viewsArray.count) {
        //这里仅仅去取最后一个高度是不是有问题? 应该遍历view后选择最高的把,这样这个高度才能是对的
        [self setupAutoHeightWithBottomView:viewsArray.lastObject bottomMargin:vInset];
    } else {
        [self clearAutoHeigtSettings];
    }
}

望采纳.

DolphinsLoveCats commented 7 years ago

之前主要考虑用于展示等宽高的一组view;如果需要实现高度不同的一组view可以换用setupAutoHeightWithBottomViewsArray:高度自适应方法,这个方法会多出一些遍历和高度比较的操作,如果有需求可以用setupAutoHeightWithBottomViewsArray:自己简单封装一套api