我在UIScrollView上遇到了一個問題,該問題似乎是iOS10特有的。我似乎在滾動視圖的頂部和內部的內容視圖之間存在差距,這應該會卡在頂部。iOS10 UIScrollView頂端的差距
在iOS 9上似乎沒有任何差距,但在iOS 10上出現了差距。
需要說明的是,在這兩種情況下,滾動視圖頂部都固定在頂部佈局指南的底部,該指南完美地與導航欄的底部對齊。 iOS 10引入了滾動視圖頂部和內容視圖頂部之間的導航欄大小的差距。
我可以將滾動視圖的頂部對齊到頂部佈局指南的頂部,這會將導航欄下方的間隙與內容視圖排成一行,但在iOS 9上,內容視圖將位於下方導航欄是不可取的。
我已經快速創建了一些演示以下問題的操場代碼。有什麼明顯的我失蹤了嗎?在iOS 10中改變了這個問題,我該如何解決這個問題?
import UIKit
import PlaygroundSupport
class TestViewController: UIViewController {
var mainScrollView: UIScrollView
var contentView: UIView
init() {
self.mainScrollView = UIScrollView()
self.contentView = UIView()
super.init(nibName: nil, bundle: nil)
self.view.backgroundColor = UIColor.white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
self.mainScrollView.backgroundColor = UIColor.green
self.contentView.backgroundColor = UIColor.blue
self.mainScrollView.translatesAutoresizingMaskIntoConstraints = false
self.contentView.translatesAutoresizingMaskIntoConstraints = false
self.view.addSubview(self.mainScrollView)
self.mainScrollView.addSubview(self.contentView)
// constrain the scroll view bounds to the view
self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.topLayoutGuide, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.bottomLayoutGuide, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.mainScrollView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
// constrain the content view bounds to the scroll view
self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.top, multiplier: 1, constant: 0))
self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.bottom, multiplier: 1, constant: 0))
self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.leading, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.leading, multiplier: 1, constant: 0))
self.mainScrollView.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.trailing, relatedBy: NSLayoutRelation.equal, toItem: self.mainScrollView, attribute: NSLayoutAttribute.trailing, multiplier: 1, constant: 0))
// constrain the content view's size to the view's size
self.view.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.width, relatedBy: NSLayoutRelation.equal, toItem: self.view, attribute: NSLayoutAttribute.width, multiplier: 1, constant: 0))
self.view.addConstraint(NSLayoutConstraint(item: self.contentView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.greaterThanOrEqual, toItem: self.view, attribute: NSLayoutAttribute.height, multiplier: 1, constant: 0))
}
}
let rootViewController = TestViewController()
rootViewController.title = "Test"
let navigationController = UINavigationController(rootViewController: rootViewController)
PlaygroundPage.current.liveView = navigationController.view
我有同樣的問題......只發生在iOS 10.你有沒有運氣修復它? – inorganik
@inorganik - 不,還沒有。現在就讓這個項目繼續下去吧。可能最終不得不放入一個操作系統版本檢查,我希望避免。 –
添加了我找到答案的解決方案,希望它對您有用。 – inorganik