gpt4 book ai didi

ios - ScrollView 不会在 Swift 中使用 AutoLayout 垂直滚动内容

转载 作者:行者123 更新时间:2023-11-28 06:46:45 26 4
gpt4 key购买 nike

我有这个 UIViewController 子类:

class MyCustomViewController: UIViewController, MyDelegate {

var scrollContainerView: UIView = {
let scrollContainerView = UIView(frame: CGRectZero)
scrollContainerView.translatesAutoresizingMaskIntoConstraints = false
return scrollContainerView
}()

var scrollView: UIScrollView = {
var scrollView = UIScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
return scrollView
}()

lazy var customView: MyCustomView = {
var customView = MyCustomView(frame: .zero)
customView.translatesAutoresizingMaskIntoConstraints = false
customView.delegate = self
return customView
}()

override func loadView() {
self.view = UIView()
self.view.backgroundColor = UIColor.whiteColor()

self.view.addSubview(self.scrollView)
self.scrollView.addSubview(self.scrollContainerView)
self.scrollContainerView.addSubview(self.customView)

view.setNeedsUpdateConstraints()
}

override func updateViewConstraints() {

self.scrollView.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.view, withOffset: 0)
self.scrollView.autoPinEdge(.Left, toEdge: .Left, ofView: self.view, withOffset: 0)
self.scrollView.autoPinEdge(.Right, toEdge: .Right, ofView: self.view, withOffset: 0)
self.scrollView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.view, withOffset: 0)

self.scrollContainerView.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.scrollView, withOffset: 0)
self.scrollContainerView.autoPinEdge(.Left, toEdge: .Left, ofView: self.scrollView, withOffset: 0)
self.scrollContainerView.autoPinEdge(.Right, toEdge: .Right, ofView: self.scrollView, withOffset: 0)
self.scrollContainerView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.scrollView, withOffset: 0)

self.customView.autoPinEdge(.Top, toEdge: .Bottom, ofView: self.scrollContainerView, withOffset: 0)
self.customView.autoPinEdge(.Left, toEdge: .Left, ofView: self.scrollContainerView, withOffset: 0)
self.customView.autoPinEdge(.Right, toEdge: .Right, ofView: self.scrollContainerView, withOffset: 0)
self.customView.autoPinEdge(.Bottom, toEdge: .Bottom, ofView: self.scrollContainerView, withOffset: 0)

super.updateViewConstraints()
}
}

其中 customView 具有固定大小。如果我在 iPhone 4S 模拟器中运行该应用程序,应该需要滚动以查看完整 View ,我发现它水平滚动,但不是垂直滚动……我不明白我会遗漏什么。

最佳答案

看来你的约束有两个问题,

  1. Set scrollContainerView EqualWidth to scrollView as you want your scroll view to be scroll vertically only.

  2. If the customView does not have subViews inside it, it could not able to calculate the height required for it, so you either need to set some points of FixedHeight to the customView or add subviews with top to bottom constraints.

其他一切正常

希望对您有所帮助。

关于ios - ScrollView 不会在 Swift 中使用 AutoLayout 垂直滚动内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36116306/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com