为了账号安全,请及时绑定邮箱和手机立即绑定

在Swift iOS中设置设备方向

在Swift iOS中设置设备方向

阿晨1998 2019-09-21 13:41:01
我正在为iPhone开发一个快速的应用程序。我的应用程序中有一个模式视图,我只想使用纵向视图。我的问题是,如何以编程方式强制手机不允许旋转?换句话说,我正在寻找的代码不允许以横向模式显示模式视图(打开人像旋转锁定)。这仅用于1个模式视图,因此我无法关闭整个应用程序的旋转,否则我将完全禁用旋转。我在这里的研究中找到了代码, 但如果有帮助,它在目标C中。谢谢!
查看完整描述

3 回答

?
qq_笑_17

TA贡献1818条经验 获得超7个赞

您可以将这些方法粘贴到每个需要纵向显示的视图的ViewController中:


override func shouldAutorotate() -> Bool {

    return false

}


override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

    return UIInterfaceOrientationMask.Portrait

}


查看完整回答
反对 回复 2019-09-21
?
杨__羊羊

TA贡献1943条经验 获得超7个赞

Hi for LandscapeLeft和LandscapeRight (更新Swift 2.0)

//img1.sycdn.imooc.com//5d85b82300014aa907580172.jpg

你有这个信息

//img1.sycdn.imooc.com//5d85b8250001072c11000376.jpg

和UIController


override func shouldAutorotate() -> Bool {

    return true

}


override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

    return [UIInterfaceOrientationMask.LandscapeLeft,UIInterfaceOrientationMask.LandscapeRight]

}

对于PortraitUpsideDown和Portrait使用 

//img1.sycdn.imooc.com//5d85b8310001136805700170.jpg

override func shouldAutorotate() -> Bool {

    if (UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeLeft ||

        UIDevice.currentDevice().orientation == UIDeviceOrientation.LandscapeRight ||

        UIDevice.currentDevice().orientation == UIDeviceOrientation.Unknown) {

            return false

    }

    else {

        return true

    }

}


override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

    return [UIInterfaceOrientationMask.Portrait ,UIInterfaceOrientationMask.PortraitUpsideDown]

}

法国寄语,圣诞快乐!


编辑:


其他解决方案:


extension UINavigationController {

    public override func shouldAutorotate() -> Bool {

        if visibleViewController is MyViewController {

            return true   // rotation

        } else {

            return false  // no rotation

        }

    }


    public override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {

        return (visibleViewController?.supportedInterfaceOrientations())!

    }

}


查看完整回答
反对 回复 2019-09-21
?
泛舟湖上清波郎朗

TA贡献1818条经验 获得超3个赞


如果将视图控制器嵌入UINavigationController或UITabBarController中,则定向旋转会更加复杂,导航或选项卡栏控制器将具有优先权并就自动旋转和支持的定向做出决策。


在UINavigationController和UITabBarController上使用以下扩展,以便嵌入在这些控制器之一中的视图控制器可以做出决定:


UINavigationController扩展


extension UINavigationController {


override open var shouldAutorotate: Bool {

    get {

        if let visibleVC = visibleViewController {

            return visibleVC.shouldAutorotate

        }

        return super.shouldAutorotate

    }

}


override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{

    get {

        if let visibleVC = visibleViewController {

            return visibleVC.preferredInterfaceOrientationForPresentation

        }

        return super.preferredInterfaceOrientationForPresentation

    }

}


override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{

    get {

        if let visibleVC = visibleViewController {

            return visibleVC.supportedInterfaceOrientations

        }

        return super.supportedInterfaceOrientations

    }

 }}

UITabBarController扩展


extension UITabBarController {


override open var shouldAutorotate: Bool {

    get {

        if let selectedVC = selectedViewController{

            return selectedVC.shouldAutorotate

        }

        return super.shouldAutorotate

    }

}


override open var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{

    get {

        if let selectedVC = selectedViewController{

            return selectedVC.preferredInterfaceOrientationForPresentation

        }

        return super.preferredInterfaceOrientationForPresentation

    }

}


override open var supportedInterfaceOrientations: UIInterfaceOrientationMask{

    get {

        if let selectedVC = selectedViewController{

            return selectedVC.supportedInterfaceOrientations

        }

        return super.supportedInterfaceOrientations

    }

}}

现在,您可以在要锁定的视图控制器中重写supportedInterfaceOrientations,shouldAutoRotate和preferredInterfaceOrientationForPresentation,否则可以在其他视图控制器中忽略要继承应用程序plist中指定的默认方向行为的替代。


锁定特定方向


class YourViewController: UIViewController {

open override var supportedInterfaceOrientations: UIInterfaceOrientationMask{

    get {

        return .portrait

    }

}}

禁用旋转


    class YourViewController: UIViewController {

open override var shouldAutorotate: Bool {

    get {

        return false

    }

}}

更改演示的首选界面方向


class YourViewController: UIViewController {

open override var preferredInterfaceOrientationForPresentation: UIInterfaceOrientation{

    get {

        return .portrait

    }

}}


查看完整回答
反对 回复 2019-09-21
  • 3 回答
  • 0 关注
  • 953 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信