2017-09-02 68 views
2

正如您可能知道的那樣,傳統的縮放方式是在UIScrollView中居中對齊,這是混亂了滾動視圖的佈局。這可以通過使用委託或UIScrollView的子類來完成。UIScrollView contentLayoutGuide and zooming centered

在WWDC 2017年視頻201(https://developer.apple.com/videos/play/wwdc2017/201/)有一個要求,新(iOS版11)contentLayoutGuide解決,而以新的方式留集中放大的問題:他們說在內容佈局的中心到中心內容視圖指南。

我這樣做並沒有解決問題。我縮放得很好,但是當縮放比例小於1時,內容視圖會像往常一樣移動到左上角。

有沒有人知道視頻中的這個聲明究竟意味着什麼? iOS 11如何使縮放比以往更容易?

編輯我實際收到的樣本項目從蘋果在迴應我的錯誤報告,說明如何解決這個問題,他們聲稱,它沒有!所以我得出結論,即使蘋果公司也不知道他們在這裏談論什麼。

+0

我看到我可以通過將可伸縮視圖的中心固定到「frameLayoutGuide」的中心來實現居中縮放,但這並不能解決問題,因爲結果顯然不是可滾動的。 – matt

+0

你有沒有想過這個?我正在努力解決同樣的問題,並找不到任何人得到這個工作。 – IMcD23

+0

@ IMcD23這不是一個真正的問題;在保持中心位置的同時,有着悠久的縮放方式。在我的問題中,問題是視頻中聲稱有一些新的方法可以實現它,涉及'contentLayoutGuide';我仍然沒有發現任何跡象支持這一說法。 – matt

回答

-1

由於未定義滾動視圖的contentSize,因此視圖會左上角。在iOS 11中使用新的自動佈局指南時,仍然需要定義contentSize。

添加以下限制:

scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor), 
scrollView.contentLayoutGuide.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor) 

這個工作對我來說,當我有一個固定的寬度/高度內容查看和下面的附加限制:

// give the centerView explicit height and width constraints 
centerView.widthAnchor.constraint(equalToConstant: 500), 
centerView.heightAnchor.constraint(equalToConstant: 500), 

// pin the center of the centerView to the center of the scrollView's contentLayoutGuide 
centerView.centerXAnchor.constraint(equalTo: scrollView.contentLayoutGuide.centerXAnchor), 
centerView.centerYAnchor.constraint(equalTo: scrollView.contentLayoutGuide.centerYAnchor) 
+0

我試過你的代碼(其實我已經嘗試過了,但是我複製了它並粘貼了它,這樣就沒有錯誤),它不起作用:當我們縮小時,我們不會保持居中,我們不會也不會連貫地滾動。一定有什麼事情你不告訴我。你可以在GitHub上發佈演示嗎?謝謝。 – matt