Tôi đang gặp sự cố với các ràng buộc trên UIScrollView dường như là iOS10 cụ thể. Tôi dường như có một khoảng cách giữa đầu của chế độ xem cuộn và chế độ xem nội dung ở bên trong, được cho là bị mắc kẹt ở đầu.Khoảng cách ở trên cùng của UIScrollView trên iOS10
Dường như không có bất kỳ khoảng trống nào trên iOS 9, nhưng trên iOS 10 khoảng trống xuất hiện.
Để rõ ràng, trong cả hai trường hợp, đầu xem cuộn được ghim vào phần dưới cùng của hướng dẫn bố cục trên cùng, đường thẳng đứng ở phía dưới cùng của thanh điều hướng một cách hoàn hảo. iOS 10 giới thiệu một khoảng trống kích thước của thanh điều hướng giữa đầu chế độ xem cuộn và phần đầu của chế độ xem nội dung.
Tôi có thể căn chỉnh đầu chế độ xem cuộn lên đầu hướng dẫn bố cục hàng đầu, điều này sẽ đặt khoảng cách bên dưới thanh điều hướng và chế độ xem nội dung sẽ xếp hàng tốt, nhưng trên iOS 9, chế độ xem nội dung sẽ ở bên dưới thanh điều hướng không mong muốn.
Tôi đã nhanh chóng tạo một số mã sân chơi thể hiện sự cố bên dưới. Có điều gì rõ ràng tôi đang thiếu? Điều gì đã thay đổi trong iOS 10 để làm cho vấn đề này xảy ra và tôi làm cách nào để giải quyết vấn đề này?
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
Tôi gặp sự cố tương tự ... chỉ xảy ra trong iOS 10. Bạn có may mắn sửa chữa nó không? – inorganik
@inorganik - Chưa, chưa. Chỉ cần di chuyển trên với dự án một để lại nó tại chỗ cho bây giờ. Có thể cuối cùng phải đưa vào một phiên bản hệ điều hành kiểm tra, mà tôi hy vọng để tránh. –
Đã thêm giải pháp mà tôi đã tìm thấy làm câu trả lời, hy vọng nó phù hợp với bạn. – inorganik