swift 开发——强制横屏或者竖屏

在AppDelegate中:app

1.添加属性:代理

    var isForceLandscape:Bool = true //标记是否横屏it

2.重写代理方法:io

func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {方法

        if self.isForceLandscape {vi

            return UIInterfaceOrientationMask.landscapeRight co

        }let

        return UIInterfaceOrientationMask.portraitcontroller

    }return

写两个全局方法:

func setHengP() {

    appDelegate.isForceLandscape = true

    let value = UIInterfaceOrientation.landscapeRight.rawValue

    UIDevice.current.setValue(value, forKey: "orientation")

}

 

func setShuP(){

    appDelegate.isForceLandscape = false

    let value = UIInterfaceOrientation.portrait.rawValue

    UIDevice.current.setValue(value, forKey: "orientation")

}

在须要横屏 或者 竖屏的controller中 的ViewWillAppear调用相应方法;在ViewWillDisappear方法中将原来设置回来;

相关文章
相关标签/搜索