How to check if device orientation is landscape left or right in swift?

前端 未结 7 1136
伪装坚强ぢ
伪装坚强ぢ 2021-02-03 21:22
    if UIDeviceOrientationIsLandscape(UIDevice.currentDevice().orientation) {
        print(\"landscape\")
    }
    if UIDeviceOrientationIsPortrait(UIDevice.currentDev         


        
7条回答
  •  情深已故
    2021-02-03 21:50

    Swift 5.0:

    UIDevice.current.orientation.isLandscape 
    UIDevice.current.orientation.isFlat
    UIDevice.current.orientation.isPortrait
    UIDevice.current.orientation.isValidInterfaceOrientation
    

    isFlat: Indicating whether the specified orientation is face up or face down (The device is held parallel to the ground with the screen facing downwards or Not).

    isValidInterfaceOrientation: Indicating whether the specified orientation is one of the portrait or landscape orientations.

    IMPORTANT NOTE:

    If you have set some views to landscape form manually, so "isLandscape" value is not correct.

    In this case you can use this condition:

    if UIScreen.main.bounds.height < UIScreen.main.bounds.width {
        print("LandScape Views")
        print("Portrait Views")
    }
    

    For example I wanted to play all videos in landscape form while my app was only for portrait form, so in video views I used this condition in order to check if the view is landscape or not, independent of the phone orientation.

提交回复
热议问题