![](/img/trans.png)
[英]Rich push notification not working in iPhone 11 Pro Max in iOS
[英]UIScrollView with glitch content on iPhone 11 Pro Max
我是 Swift 的新手,我嘗試制作顯示視圖控制器的UIScrollView
。
iPhone 11 Pro Max 上的每一件事都很完美,下一個屏幕在側面顯示了一點點:
我的代碼:
//MARK: - outlets
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var scrollView: UIScrollView!
//MARK: - properties
var viewControllers: [String] = ["ComputerViewController", "AttactViewController", "DefenceViewController", "OfflineViewController"]
var frame = CGRect(x: 0, y: 0, width: 0, height: 0)
//MARK: - life cyrcles
override func viewDidLoad() {
super.viewDidLoad()
for index in 0..<viewControllers.count {
frame.origin.x = scrollView.frame.size.width * CGFloat(index)
frame.size = scrollView.frame.size
let view = UIView(frame: frame)
let storyboard = UIStoryboard(name: "Menu", bundle: nil)
var controller: UIViewController = storyboard.instantiateViewController(withIdentifier: viewControllers[index]) as UIViewController
view.addSubview(controller.view)
self.scrollView.addSubview(view)
}
scrollView.contentSize = CGSize(width: scrollView.frame.size.width * CGFloat(viewControllers.count), height: scrollView.frame.size.height)
scrollView.delegate = self
}
func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
var pageNumber = scrollView.contentOffset.x / scrollView.frame.size.width
pageControl.currentPage = Int(pageNumber)
}
感謝您的幫助...
幾點觀察:
您應該避免在viewDidLoad
中引用frame
。 在這一點上, frame
是未知的。
您應該完全避免對子視圖的大小和位置進行硬編碼。 稍后可能會有多種事件改變視圖的大小(例如旋轉、拆分視圖多任務等)。 使用約束。
對於滾動視圖,有兩個布局指南,一個用於其frame
,另一個用於其contentSize
。 因此,使用frameLayoutGuide
設置子視圖的大小以及這些子視圖相對於contentLayoutGuide
的位置。
當您將視圖控制器的view
添加為子視圖時,請確保調用addChild(_:)
和didMove(toParent:)
調用以包含視圖 controller。 請參閱視圖 controller 文檔的“實現容器視圖控制器”部分。
如果要添加分頁行為,只需設置滾動視圖的isPagingEnabled
。
將所有這些放在一起:
override func viewDidLoad() {
super.viewDidLoad()
addChildViews()
}
override func viewDidLoad() {
super.viewDidLoad()
var anchor = scrollView.contentLayoutGuide.leadingAnchor
for identifier in viewControllers {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let child = storyboard.instantiateViewController(withIdentifier: identifier)
addChild(child) // containment call
child.view.translatesAutoresizingMaskIntoConstraints = false
scrollView.addSubview(child.view)
child.didMove(toParent: self) // containment call
NSLayoutConstraint.activate([
// define size of child view (relative to `frameLayoutGuide`)
child.view.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
child.view.heightAnchor.constraint(equalTo: scrollView.frameLayoutGuide.heightAnchor),
// define placement of child view (relative to `contentLayoutGuide`)
child.view.leadingAnchor.constraint(equalTo: anchor),
child.view.topAnchor.constraint(equalTo: scrollView.contentLayoutGuide.topAnchor),
child.view.bottomAnchor.constraint(equalTo: scrollView.contentLayoutGuide.bottomAnchor),
])
anchor = child.view.trailingAnchor
}
anchor.constraint(equalTo: scrollView.contentLayoutGuide.trailingAnchor).isActive = true
scrollView.isPagingEnabled = true
}
我已經消除了不再需要的屬性frame
(並且只是與視圖控制器的視圖的同名屬性混淆的來源)。 我還刪除了容器視圖,因為它並沒有為整體解決方案添加太多內容(並且只添加了一層要添加的約束)。
但關鍵是使用約束來規定滾動視圖中子視圖的大小和 position,並使用視圖 controller 包含 API。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.