UIViewController 未在所有 iPhone 的 UIScrollView 中正确调整

UIViewController not adjusting correctly in UIScrollView for all iPhones

我正在尝试复制类似 Tinder 的菜单,在 UIScrollView 中有 3 个 UIViewControllers,在顶部有一个自定义菜单选项卡,每个 UIViewController 都有一个按钮。我面临一个有趣的问题,其中 UIViewControllers 视图完全适合 scrollView.frame,但仅适用于 iPhone 8。相比之下,对于 iPhone SE,它留下了空白边距对于 iPhone 8+,它似乎与 scrollView.view 中的视图重叠。有人可以解释为什么会发生这种情况以及我该如何解决吗?

这是我在 UIScrollView 中调整 UIViewControllers 设置的代码:

import UIKit

class MainViewController: UIViewController {

    @IBOutlet weak var scrollView: UIScrollView!
    @IBOutlet weak var navigationView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()
        setUpHorizontalScrollViews()
    }

    func setUpHorizontalScrollViews(){

        let view = (
            x: self.view.bounds.origin.x,
            y: self.view.bounds.origin.y,
            width: self.view.bounds.width,
            height: self.view.bounds.height
        )

        let scrollWidth  = 3 * view.width
        let scrollHeight  = view.height
        scrollView.contentSize = CGSize(width: scrollWidth, height: scrollHeight)
        scrollView.contentOffset.x = view.x

        if let messagesView = self.storyboard?.instantiateViewController(withIdentifier: "MessagesVC") as UIViewController! {
            self.addChildViewController(messagesView)
            self.scrollView.addSubview(messagesView.view)
            messagesView.didMove(toParentViewController: self)
            messagesView.view.frame = CGRect(x: 0,
                                             y: 0,
                                             width: view.width,
                                             height: view.height
            )
        }


        if let friendsView = self.storyboard?.instantiateViewController(withIdentifier: "FriendsVC") as UIViewController! {
            self.addChildViewController(friendsView)
            self.scrollView.addSubview(friendsView.view)
            friendsView.didMove(toParentViewController: self)
            friendsView.view.frame = CGRect(x: view.width,
                                            y: 0,
                                            width: view.width,
                                            height: view.height
            )
        }


        if let settingsView = self.storyboard?.instantiateViewController(withIdentifier: "SettingsVC") as UIViewController! {
            self.addChildViewController(settingsView)
            self.scrollView.addSubview(settingsView.view)
            settingsView.didMove(toParentViewController: self)
            settingsView.view.frame = CGRect(x: 2 * view.width,
                                             y: 0,
                                             width: view.width,
                                             height: view.height
            )
        }

        // offset to the second view
        self.scrollView.contentOffset.x = view.width

    }

    override var shouldAutorotate: Bool {
        return false
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

这是我的安装程序的样子。顶部是 MainViewController,包含 UIScrollView,底部是应该进入滚动视图的 3 个视图控制器。

这就是我想要的样子,以及它在 iPhone 8 中的设置方式:

这是 iPhone SE 上的样子,我的问题是:

问题是因为你在viewDidLoad()中调用setUpHorizontalScrollViews方法,UIViewController还没有布局子视图,也没有计算它们的最终大小。

它在 iPhone 8 中工作,因为最有可能证明它的屏幕尺寸与您在界面生成器中使用的屏幕尺寸相同。

解决方案 1

为了解决问题,您可以将代码移至viewDidAppear()方法。但是,这会在您打开 UIViewController 时造成难看的效果(除非您添加全屏加载)。

解决方案 2

像这样添加view.layourIfNeeded()

override func viewDidLoad() {
    super.viewDidLoad()
    view.layourIfNeeded()
    setUpHorizontalScrollViews()
}