简体   繁体   English

有没有一种方法可以将UIScrollView嵌入UIStackView中?

[英]Is there a way to embed a UIScrollView within a UIStackView?

Xcode故事板:UIViewController结构 Xcode故事板:内部UIScrollView错误消息

I am working on a ViewController that has the following structure: 我正在使用具有以下结构的ViewController:

-ViewController
 -UIScrollView
  -UIVerticalStackView
    -UIImageView (414:367 aspect ratio)
    -UILabel
    -UIScrollView
     -UIHorizontalStackView
      -Dynamically added UIButtons
    -UILabel
    -UITextView
    -UIButton

There are more elements on the ViewController than can fit on the screen which is why I want to be able to vertically scroll through the elements. ViewController上的元素过多,无法容纳在屏幕上,这就是为什么我希望能够垂直滚动这些元素。 However, one of those elements will contain dynamically generated/added UIButtons. 但是,这些元素之一将包含动态生成/添加的UIButton。 Because the number of UIButtons may exceed the width of the screen, I want those UIButtons to be embedded within a UIScrollView that enables a user to horizontally scroll through them if needed, hence the use of the inner UIScrollView. 因为UIButton的数量可能超过了屏幕的宽度,所以我希望将这些UIButton嵌入到UIScrollView中,该UIScrollView使用户可以在需要时水平滚动它们,因此可以使用内部UIScrollView。 For formatting purposes, I add the UIButtons to a UIHorizontalStackView which is nested inside the inner UIScrollView. 为了格式化,我将UIButtons添加到嵌套在内部UIScrollView内的UIHorizo​​ntalStackView中。

However, I am getting an error (as seen in the second attached image). 但是,我遇到了一个错误(如第二幅图像所示)。

- -

You haven't provided any constraints for the "inner stack view" contained in the "inner scroll view". 您尚未为“内部滚动视图”中包含的“内部堆栈视图”提供任何约束。

What you most likely want is for the "inner stack view" to have the same height as "inner scroll view" but no width constraint, allowing your dynamically added buttons to fill from left to right, and then scroll horizontally when there are too many to fit. 您最可能希望的是“内部堆栈视图”具有与“内部滚动视图”相同的高度,但没有宽度限制,从而允许动态添加的按钮从左到右填充,然后在过多的按钮时水平滚动适合。

So: 所以:

在此处输入图片说明

You can see in this image that "inner stack view" is constrained on all 4 sides to "inner scroll view" and is also constrained to the width and height: 您可以在此图像中看到,“内部堆栈视图”在所有4个侧面都被限制为“内部滚动视图”,并且还被限制为宽度和高度:

Inner Stack View.height = height
Inner Stack View.width = width

(the height = 100 is the height constraint on "inner scroll view") height = 100是“内部滚动视图”上的高度限制)

What cannot be seen here is that you don't really want the width constraint - you want the stack view to "fill with buttons" and extend horizontally past the right edge when needed. 在这里看不到的是您实际上并不需要宽度限制-您希望堆栈视图“填充按钮”并在需要时水平延伸超过右边缘。

To do that, select the Inner Stack View.width = width constraint, and set it as a Placeholder / Remove at build time : 为此,选择Inner Stack View.width = width约束,并将其设置为Placeholder / Remove at build time

在此处输入图片说明

This will keep IB / Storyboard happy (all necessary constraints are satisfied), but will give you the layout flexibility you want at run-time. 这将使IB / Storyboard保持满意(满足所有必要的约束),但将为您提供运行时所需的布局灵活性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM