Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wrong device-orientation returned

Tags:

I think I found a bug in the UIDevice.orientation variable.

override func viewDidAppear(_ animated: Bool) {
    super.viewDidAppear(true)
    if UIDevice.current.orientation.isPortrait{
        print("Orientation is Portrait")
    }
    else{
        print("Orientation is Landscape")
    }
}

Everything works fine if I hold the device(Iphone 5) in my hand, but if it lies flat on the table I get "Orientation is Landscape" even if everything is shown in Portrait and the device itself is not rotated. Is there any possible way to do something against this?

// Edit: This test also fails within button-actions.

like image 246
LarsGvB Avatar asked Dec 23 '17 18:12

LarsGvB


2 Answers

There are more orientations besides portrait and landscape. You are checking for portrait and assuming everything else is landscape. It is not. In this case, the actual orientation is likely “face up”.

like image 162
picciano Avatar answered Sep 23 '22 13:09

picciano


There is a third option UIDevice.current.orientation.isFlat beside portrait and landscape and You are checking only for portrait

like image 31
iKristof Avatar answered Sep 19 '22 13:09

iKristof