Get current orientation of iPad?

后端 未结 11 1711
野趣味
野趣味 2020-12-02 07:28

In a given event handler (not the \"shouldAutorotateToInterfaceOrientation\" method) how do I detect the current iPad orientation? I have a text field I have to animate up

11条回答
  •  抹茶落季
    2020-12-02 08:06

    [UIDevice currentDevice].orientation works great.

    BUT!!! ... the trick is to add it to - (void)viewWillAppear:(BOOL)animated

    exp:

    (void)viewWillAppear:(BOOL)animated{
       ...
       BOOL isLandscape = UIInterfaceOrientationIsLandscape(self.interfaceOrientation);
       ...
    }
    

    If you call it at - (void)viewDidLoad, it does not work reliable, especially if you use multiple threads (main UI thread, background thread to access massive external data, ...).


    Comments: 1) Even if your app sets default orientation portrait, user can lock it at landscape. Thus setting the default is not really a solution to work around it. 2) There are other tasks like hiding the navigation bar, to be placed at viewWillAppear to make it work and at the same time prevent flickering. Same applies to other views like UITableView willDisplayCell -> use it to set cell.selected and cell.accessoryType.

提交回复
热议问题