ios - UIScrollView 作为辅助 View 的问题

标签 ios swift uiscrollview interface-builder

我已经为此工作了几个小时,但我无法从作为辅助 View 的 UIScrollView 中获得所需的行为。我有一个名为 newDogView 的辅助 View ,其中包含 3 个堆栈 View 。我添加了 newDogView 并在代码中设置了它的约束。

我的目的是将 newDogView (960x320)呈现为一个较小的 View (320x320),并且能够水平滚动。当前的结果是 View 已添加,但仍保持 960x320。我认为困惑要么来自堆栈 View ,要么来自它是辅助 View 而不是 View Controller 内部的事实。

相关代码如下:

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 的高度和宽度。

这是我当前的 View 层次结构和警告:

Here is my view hierarchy and warning.

这是我当前的 Storyboard:

Here is my storyboard.

我在内容 View ScrollView 的所有侧面都固定了约束。

以下是我的 iPhone 上当前的 View : iphone photo

最佳答案

尝试更改内容大小

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

关于ios - UIScrollView 作为辅助 View 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45992823/

相关文章:

swift - 无法使用类型为 '<' 的参数列表调用 '(T, T)'

objective-c - iOS OpenGL 层和 UIScrollView

iOS 胖二进制文件和细化

ios - swift 中的 UICTContentSizeCategory

ios:将新密码添加到 iCloud 钥匙串(keychain)

iphone - UIGestureRecognizer 未触发的可能原因?

ios - Swift 中 UIScrollView 内的 UITableView 高度

ios - 如何处理取消的iOS应用内购买自动续订订阅

ios - UIImageView 显示不正确

java - 新的 Apple 登录不断抛出错误 HTTP 400 Invalid_grant