iPad旋转至横向

问题描述

使用iOS14.1,Swift5.3和

我尝试将iPad从纵向旋转到横向。但是,在横向模式下,视图的一侧是黑色的(请参见视频)。

要旋转iPad我需要做什么?

enter image description here

    open override var shouldAutorotate: Bool {
        return true
    }
    
    open override func viewWillTransition(to size: CGSize,with coordinator: UIViewControllerTransitionCoordinator) {
        
        super.viewWillTransition(to: size,with: coordinator)

        coordinator.animate(alongsideTransition: { (context) in
            guard let windowInterfaceOrientation = self.windowInterfaceOrientation else { return }

            if windowInterfaceOrientation.isLandscape {
                // activate landscape changes
                print("landscape Now")
                print(UIScreen.main.bounds.height)
                print(UIScreen.main.bounds.width)
                self.view.frame = CGRect(x: 0.0,y: 0.0,width: UIScreen.main.bounds.width,height: UIScreen.main.bounds.height)
                self.view.setNeedsLayout()
                self.view.layoutSubviews()
            } else {
                // activate portrait changes
                print("portrait Now")
                print(UIScreen.main.bounds.height)
                print(UIScreen.main.bounds.width)
                self.view.frame = CGRect(x: 0.0,height: UIScreen.main.bounds.height)
                self.view.setNeedsLayout()
                self.view.layoutSubviews()
            }
        })
    }

    private var windowInterfaceOrientation: UIInterfaceOrientation? {
        if #available(iOS 13.0,*) {
            return UIApplication.shared.windows.first?.windowScene?.interfaceOrientation
        } else {
            return UIApplication.shared.statusBarOrientation
        }
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)