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方法中將原來設置回來;

相關文章
相關標籤/搜索