OGeek|极客世界-中国程序员成长平台

标题: iphone - iOS 自动布局 : Resizing container with constraints [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 11:17
标题: iphone - iOS 自动布局 : Resizing container with constraints

我有一个 ScrollView ,里面有容器 View (self.tagScrollContentView)。那是在 Storyboard中。然后我生成按钮并以编程方式将它们放置在带有约束的容器 View 中。

for(NSInteger i = 0; i < allTags.count; i++) {
   UIButton *tagBt = [[UIButton alloc] initWithFrameCGRect){CGPointZero, tagSize.width + 30, 17}];
   [self.tagScrollContentView addSubview:tagBt];

   [constraintsArray addObject:[NSLayoutConstraint constraintWithItem:tagBt attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:nil multiplier:1.0 constant:tagSize.width + 30]];

   if(prevBtRow1)
      [constraintsArray addObject:[NSLayoutConstraint constraintWithItem:tagBt attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:prevBtRow1 attribute:NSLayoutAttributeRight multiplier:1.0 constant:10.0]];
   else
      [constraintsArray addObject:[NSLayoutConstraint constraintWithItem:tagBt attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.tagScrollContentView attribute:NSLayoutAttributeLeading multiplier:1.0 constant:10.0]];

   [constraintsArray addObject:[NSLayoutConstraint constraintWithItem:tagBt attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tagScrollContentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:7.0]];

   prevBtRow1 = tagBt;
}

[self.tagScrollContentView addConstraints:constraintsArray];
[self.tagScrollView layoutSubviews];

此代码将所有按钮排成一行,具体取决于它们的宽度。一切正常。然后我需要放大 tagScrollContentView 以使所有按钮都在此 View 内而不是在边界之外。然后为我的滚动分配等于容器 View 的正确内容大小。 不幸的是,滚动无法正常工作。内容大小不适合容器 View 。



Best Answer-推荐答案


关键问题是您的 contentSize 没有设置,因为您没有将最后一个按钮从最后一个按钮添加到其 super View 。您可以在最后添加一个约束,您的 contentSize 将自动调整:

for (NSInteger i = 0; i < allTags.count; i++) {
    UIButton *tagBt = [[UIButton alloc] init];
    tagBt.translatesAutoresizingMaskIntoConstraints = NO;
    [self.tagScrollContentView addSubview:tagBt];

    // add all of your constraints

    prevBtRow1 = tagBt;
}

[constraintsArray addObject:[NSLayoutConstraint constraintWithItem:prevBtRow1
                                                         attribute:NSLayoutAttributeTrailing
                                                         relatedBy:NSLayoutRelationEqual
                                                            toItem:self.tagScrollContentView
                                                         attribute:NSLayoutAttributeTrailing
                                                        multiplier:1.0
                                                          constant:10.0]];

[self.tagScrollContentView addConstraints:constraintsArray];

有几个不相关的问题:

  1. 我假设您有一个 tagBt.translatesAutoresizingMaskIntoConstraints = NO; 行没有进入您的代码示例。

  2. 如果您要设置约束,那么执行 initWithFrame 毫无意义。 init 就足够了。

  3. 我建议您也为您的按钮添加一个高度约束,这样它的约束就会变得明确。

  4. 顺便说一下,您正在向 super View 添加按钮宽度约束。无论哪种方式,它都可以工作,但通常您向最近的公共(public)父级添加一个约束,对于宽度约束,这将是按钮本身,而不是它的父 View 。

关于iphone - iOS 自动布局 : Resizing container with constraints,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16759212/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4