In Swift, how to get the device orientation correctly right after it's launched?

后端 未结 5 1831
孤独总比滥情好
孤独总比滥情好 2021-01-12 22:45

I use following code to see if the device is in landscape mode or not:

UIDevice.currentDevice().orientation.isLandscape.boolValue

It works

5条回答
  •  情话喂你
    2021-01-12 23:29

    The isValidInterfaceOrientation should be detected before checking the orientation isLandscape. Don't process the flat message with isValidInterfaceOrientation == false (when it has any value of isLandscape).

    I had a hazzel with this until I read the topic more carefully. With consideration of the isValidInterfaceOrientation it works fine.

    @objc func rotated() {
        if (UIDevice.current.orientation.isValidInterfaceOrientation) {
            if (UIDevice.current.orientation.isLandscape) { 
                if(!bLandscape) {
                    bLandscape = true
                    setupTabBar() // Repaint the app
                }
            } else { // Portait 
                if(bLandscape) {
                    bLandscape = false
                    setupTabBar() // Repaint the app 
                }
            }
        }
    }
    

提交回复
热议问题