将UIScrollView作为辅助视图发生故障
我一直在为此工作了几个小时,而且我无法从UIScrollView
中获得期望的行为作为辅助视图。我有一个名为newDogView
的辅助视图,其中包含3个堆栈视图。我添加newDogView
并设置它在代码中的约束。将UIScrollView作为辅助视图发生故障
我的意图是呈现newDogView
(这是960x320)作为一个较小的看法(320x320)与水平滚动的能力。目前的结果是添加了视图,但保持960x320。我认为困惑不是来自堆栈视图,就是它是次要视图,而不是视图控制器内部。
下面是相关代码:
override func viewWillLayoutSubviews() {
newDogView.translatesAutoresizingMaskIntoConstraints = false
newDogScrollView.translatesAutoresizingMaskIntoConstraints = false
newDogScrollView.heightAnchor.constraint(equalToConstant: 320.0).isActive = true
newDogScrollView.widthAnchor.constraint(equalToConstant: 960.0).isActive = true
newDogScrollView.contentSize = CGSize(width: 320, height: 320)
newDogView.heightAnchor.constraint(equalToConstant: 320.0).isActive = true
newDogView.widthAnchor.constraint(equalToConstant: 320.0).isActive = true
newDogView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
newDogView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
我不知道我是否应该设置newDogView的高度和宽度。
这里是我当前视图层次和警告:
这是我目前的分镜:
我别在各方对Content View
和Scroll View
限制。
尝试从
CGSize(width: 320, height: 320)
到
CGSize(width: 960, height: 320)
改变内容的大小,改变
newDogView.widthAnchor.constraint(equalToConstant: 960.0).isActive = true
到
newDogView.widthAnchor.constraint(equalToConstant: 320.0).isActive = true
它似乎确实像UIScrollViews很纠结得到正确的。在我看来,视图的宽度应该是你希望可见窗口的宽度。内容大小应该是滚动视图内的东西的总宽度/高度。整齐!谢谢你的帮助。 –
@CodyPotter是的,你已经明白了,你总是欢迎,欢呼声 – 3stud1ant3
如果你设置高度和宽度,那么这是什么行为? – 3stud1ant3
您需要确保您有约束条件才能隐式或显式设置滚动视图的大小,并且需要设置滚动视图的内容大小属性,以便可以正确调整内容的大小。 – Paulw11
@ Paulw11请查看我更新的代码和编辑。 –