iOS9:UIScrollView又坏了?
iOS 9: UIScrollView is broken again?
我注意到 UIScrollView
可能又坏了。我正在使用 OS X 10.11.1
和 Xcode 7 (from AppStore)
或 Xcode 7.1 beta
。 AutoLayout 完成工作后,contentOffset
属性 不在 UIScrollView
的 origin
处。
有人可以确认这个问题吗?
然而,我设法解决了这个问题,但这并不是它应该的样子:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.scrollView.contentOffset = CGPoint(x: 0, y: -self.scrollView.frame.origin.y)
/* if you need a different position, I suggest to safe the
contentOffset inside viewWillLayoutSubviews and calculate the offset here */
}
重现问题的方法如下:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
private let scrollView = UIScrollView()
private let containerView = UIView()
override func loadView() {
self.view = UIView()
self.view.backgroundColor = UIColor.whiteColor()
}
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.layer.borderColor = UIColor.greenColor().CGColor
self.scrollView.layer.borderWidth = 1.0
self.scrollView.clipsToBounds = false
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
self.scrollView.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor).active = true
self.scrollView.heightAnchor.constraintEqualToAnchor(self.view.widthAnchor).active = true
self.scrollView.widthAnchor.constraintEqualToAnchor(self.view.widthAnchor).active = true
self.containerView.backgroundColor = UIColor.redColor()
self.containerView.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.addSubview(self.containerView)
self.containerView.widthAnchor.constraintEqualToAnchor(self.scrollView.widthAnchor).active = true
self.containerView.heightAnchor.constraintEqualToAnchor(self.scrollView.heightAnchor).active = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("content offset: \(self.scrollView.contentOffset)")
print("origin: \(self.scrollView.frame.origin)")
print("they to not match visually :( <-- BROKEN !!!")
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func prefersStatusBarHidden() -> Bool {
return false
}
}
更新: 已报告并填充了雷达:22770934
更新 2:问题已解决。
我在视图调试器的帮助下找到了解决方案。
添加这个缺失的约束,一切都会好起来的:
self.containerView.topAnchor.constraintEqualToAnchor(self.scrollView.topAnchor).active = true
self.containerView.leftAnchor.constraintEqualToAnchor(self.scrollView.leftAnchor).active = true
self.containerView.rightAnchor.constraintEqualToAnchor(self.scrollView.rightAnchor).active = true
self.containerView.bottomAnchor.constraintEqualToAnchor(self.scrollView.bottomAnchor).active = true
// actually the debugger showed two different constraints, but this had no effect on the functionality, correct me here if I am wrong
// trailing and button constraints where like this:
// self.scrollView.trailingAnchor.constraintEqualToAnchor(self.containerView.trailingAnchor).active = true
// self.scrollView.bottomAnchor.constraintEqualToAnchor(self.containerView.bottomAnchor).active = true
我注意到 UIScrollView
可能又坏了。我正在使用 OS X 10.11.1
和 Xcode 7 (from AppStore)
或 Xcode 7.1 beta
。 AutoLayout 完成工作后,contentOffset
属性 不在 UIScrollView
的 origin
处。
有人可以确认这个问题吗?
然而,我设法解决了这个问题,但这并不是它应该的样子:
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
self.scrollView.contentOffset = CGPoint(x: 0, y: -self.scrollView.frame.origin.y)
/* if you need a different position, I suggest to safe the
contentOffset inside viewWillLayoutSubviews and calculate the offset here */
}
重现问题的方法如下:
import UIKit
class ViewController: UIViewController, UIScrollViewDelegate {
private let scrollView = UIScrollView()
private let containerView = UIView()
override func loadView() {
self.view = UIView()
self.view.backgroundColor = UIColor.whiteColor()
}
override func viewDidLoad() {
super.viewDidLoad()
self.scrollView.layer.borderColor = UIColor.greenColor().CGColor
self.scrollView.layer.borderWidth = 1.0
self.scrollView.clipsToBounds = false
self.scrollView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(scrollView)
self.scrollView.topAnchor.constraintEqualToAnchor(self.topLayoutGuide.bottomAnchor).active = true
self.scrollView.heightAnchor.constraintEqualToAnchor(self.view.widthAnchor).active = true
self.scrollView.widthAnchor.constraintEqualToAnchor(self.view.widthAnchor).active = true
self.containerView.backgroundColor = UIColor.redColor()
self.containerView.translatesAutoresizingMaskIntoConstraints = false
self.scrollView.addSubview(self.containerView)
self.containerView.widthAnchor.constraintEqualToAnchor(self.scrollView.widthAnchor).active = true
self.containerView.heightAnchor.constraintEqualToAnchor(self.scrollView.heightAnchor).active = true
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
print("content offset: \(self.scrollView.contentOffset)")
print("origin: \(self.scrollView.frame.origin)")
print("they to not match visually :( <-- BROKEN !!!")
}
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
return UIInterfaceOrientationMask.Portrait
}
override func prefersStatusBarHidden() -> Bool {
return false
}
}
更新: 已报告并填充了雷达:22770934
更新 2:问题已解决。
我在视图调试器的帮助下找到了解决方案。
添加这个缺失的约束,一切都会好起来的:
self.containerView.topAnchor.constraintEqualToAnchor(self.scrollView.topAnchor).active = true
self.containerView.leftAnchor.constraintEqualToAnchor(self.scrollView.leftAnchor).active = true
self.containerView.rightAnchor.constraintEqualToAnchor(self.scrollView.rightAnchor).active = true
self.containerView.bottomAnchor.constraintEqualToAnchor(self.scrollView.bottomAnchor).active = true
// actually the debugger showed two different constraints, but this had no effect on the functionality, correct me here if I am wrong
// trailing and button constraints where like this:
// self.scrollView.trailingAnchor.constraintEqualToAnchor(self.containerView.trailingAnchor).active = true
// self.scrollView.bottomAnchor.constraintEqualToAnchor(self.containerView.bottomAnchor).active = true