【问题标题】:UIViewController not adjusting correctly in UIScrollView for all iPhonesUIViewController 在所有 iPhone 的 UIScrollView 中没有正确调整
【发布时间】:2018-03-01 22:33:12
【问题描述】:

我正在尝试复制一个类似 Tinder 的菜单,其中 3 个 UIViewControllersUIScrollView 中,顶部有一个自定义菜单选项卡,每个 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,底部是应该进入 scrollView 的 3 个视图控制器。

这是我希望它的样子,以及它在 iPhone 8 中的设置方式:

这是它在 iPhone SE 上的样子,也是我的问题所在:

【问题讨论】:

    标签: swift uiview uiviewcontroller uiscrollview frame


    【解决方案1】:

    问题在于,由于您在viewDidLoad() 中调用setUpHorizontalScrollViews 方法,因此 UIViewController 尚未对子视图进行布局,也尚未计算它们的最终大小。

    它可以在 iPhone 8 上运行,因为很可能它的屏幕尺寸与您在界面构建器中使用的屏幕尺寸相同。

    解决方案 1

    为了解决问题,您可以将代码移至viewDidAppear() 方法。但是,一旦您打开 UIViewController,这将导致丑陋的效果(除非您添加全屏加载)。

    解决方案 2

    像这样添加view.layourIfNeeded()

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

    【讨论】:

    • 有效,感谢您的解释,我不知道这一点。我选择使用 viewDidAppear() 选项,因为代码更简洁 -> 虽然没有丑陋的效果
    猜你喜欢
    • 2012-02-04
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    相关资源
    最近更新 更多