【问题标题】:How to configure a UIScrollView content size to have a fixed width and a dynamic height?如何将 UIScrollView 内容大小配置为具有固定宽度和动态高度?
【发布时间】:2014-01-13 17:16:20
【问题描述】:

我有一个 UIScrollView 包含在另一个容器视图中,其约束设置为占用容器视图的所有空间。也就是说我在滚动视图上没有固定的宽度或高度。
在我的滚动视图中,我以编程方式添加子视图。每个子视图(内容视图)都是从 xib 加载的。
在 xib 中,我将任意大小设置为根视图 (500x500),但我希望该视图宽度自动调整为滚动视图宽度(滚动视图宽度是容器宽度)。
我不希望用户能够水平滚动。

我尝试了不同的解决方案,总是导致滚动视图可以水平滚动。
我试图将内容视图的拥抱和压缩属性调整为水平轴上的不同值,但没有成功。

我不想在我的视图上设置固定宽度,因为我希望它们采用容器视图的宽度。

如果您有任何建议,请提前感谢。

【问题讨论】:

    标签: ios uiscrollview autolayout


    【解决方案1】:

    我当时想出的答案晚了。
    由于我使用的是 Autolayout,所以 VChemezov 的回答并不令人满意。

    我的内容视图有一组顶部、底部、前导、宽度约束。 (宽度而不是尾随,这是我首先要做的,但它不起作用)。

    所以现在我有这样的东西:

            NSLayoutConstraint *leading = [NSLayoutConstraint
                                           constraintWithItem:messageView
                                           attribute:NSLayoutAttributeLeading
                                           relatedBy:NSLayoutRelationEqual
                                           toItem:self.conversationScrollView
                                           attribute:NSLayoutAttributeLeading
                                           multiplier:1.0f
                                           constant:0.0f];
            NSLayoutConstraint *width = [NSLayoutConstraint
                                            constraintWithItem:messageView
                                            attribute:NSLayoutAttributeWidth
                                            relatedBy:NSLayoutRelationEqual
                                            toItem:self.conversationScrollView
                                            attribute:NSLayoutAttributeWidth
                                            multiplier:1.0f
                                            constant:0.0f];
            [self.conversationScrollView addConstraints:@[ top, leading, width ]]; 
    

    内容视图的宽度等于滚动视图的宽度。

    【讨论】:

    • 这对我有帮助,我只是想补充一点,您可以通过将内部视图(在本例中为 messageView)附加到滚动视图的所有四个侧面来在 Storyboards 中复制此行为,然后添加一个scrollView 的等宽约束。如果scrollView附加到superview,这会自动设置contentView的宽度为superview的宽度。也处理方向更新。
    【解决方案2】:

    自动布局不会调整 UIScrollView 内的视图大小。您应该自己布局和调整这些视图的大小。有两种基本的解决方案:

    • 在添加到 UIScrollView 之前显式设置子视图的宽度
    • 继承 UIScrollView 并像这样覆盖 setFramelayoutSubviews

      @implementation MyScrollView
      
      - (void)setFrame:(CGRect)frame{
          [super setFrame:frame];
          [self setNeedsLayout];
      }
      
      - (void)layoutSubviews{
          [super layoutSubviews];
          NSArray * subviews=self.subviews;
          for(UIView * view in subviews){
              CGRect viewFrame=view.frame;
              viewFrame.size.width=self.bounds.size.width;
              view.frame=viewFrame;
          }
      }
      
      @end
      

    【讨论】:

      【解决方案3】:

      使用SnapKit 会更快:

      let helper = UIView()
      scrollView.addSubview(helper)
      
      helper.snp.makeConstraints { make in
          make.width.equalTo(snp.width)
          make.leading.trailing.top.equalToSuperview()
          make.height.equalTo(0)
      }
      

      【讨论】:

      • +1 因为这绝对是现在要走的路。 2014 => 2017. 使用 SnapKit 或 PureLayout 但不要使用原生 Autolayout API(除非你有太多时间)。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2021-02-27
      • 2021-10-19
      • 2018-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多