iOS: Device orientation on load

前端 未结 16 1916
遥遥无期
遥遥无期 2020-11-28 23:45

It seems that when my app loads, it does not know its current orientation:

UIInterfaceOrientation orientation = [[UIDevice currentDevice] orientation];
if (o         


        
16条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-29 00:09

    for those who looking answer for Swift 3 or 4. just add that code inside of viewDidLoad() block.

    let orientation = UIApplication.shared.statusBarOrientation
    if orientation == .portrait {
          // portrait   
    } else if orientation == .landscapeRight || orientation == .landscapeLeft{
          // landscape     
    }
    

    update for depreciation alerts for IOS 13 and Swift 5.x use code block below.

     if #available(iOS 13.0, *) {
            let orientation = UIApplication.shared.windows.first?.windowScene?.interfaceOrientation
            if orientation == .portrait {
                         // portrait
    
                   } else if orientation == .landscapeRight || orientation == .landscapeLeft{
                         // landscape
                   }
        } else {
            // Fallback on earlier versions
            let orientation = UIApplication.shared.statusBarOrientation
            if orientation == .portrait {
                                // portrait
    
        } else if orientation == .landscapeRight || orientation == .landscapeLeft{
                                   // landscape
        }
        }
    

提交回复
热议问题