方向改变不起作用

Chi*_*tel 1 iphone orientation screen-orientation orientation-changes

提前致谢

我想检测我的设备中何时更改了方向...因为我使用了这个监听器

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:@"UIDeviceOrientationDidChangeNotification" object:nil];
Run Code Online (Sandbox Code Playgroud)

和方向改变的方法是

- (void) orientationChanged:(id)obj
{  
    if( [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft || [UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight )
    {
        self.view=self.landscapeView;
        NSLog(@"landscapeView");

       //[self loadView1];
    }
    else 
    {
       self.view = self.portraitView;
       NSLog(@"portraitView");

       //[self loadView1];
    }    
}
Run Code Online (Sandbox Code Playgroud)

并且每次进入ViewDidLoad()方法的方向...

谁能帮我..

Krr*_*ish 6

你的问题很模糊,这是一般性的解释.您可以覆盖下面提到的方法来处理方向更改

超越下面提到的方法

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
   // return YES to supported orientation.
}

 - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
/* Subclasses may override this method to perform additional actions immediately 
   prior to the rotation. For example, you might use this method to disable view 
   interactions, stop media playback, or temporarily turn off expensive drawing or live 
   updates. You might also use it to swap the current view for one that reflects the new
   interface orientation. When this method is called, the interfaceOrientation property 
   still contains the view’s original orientation. 
*/

}

- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
    /*Subclasses may override this method to perform additional actions immediately after
      the rotation. For example, you might use this method to reenable view interactions, 
      start media playback again, or turn on expensive drawing or live updates. By the time 
      this method is called, the interfaceOrientation property is already set to the new 
      orientation.*/
}


- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
    /*This method is called from within the animation block used to rotate the view. 
      You can override this method and use it to configure additional animations that should 
      occur during the view rotation. For example, you could use it to adjust the zoom level 
      of your content, change the scroller position, or modify other animatable properties
      of your view.*/
}
Run Code Online (Sandbox Code Playgroud)

请勿在viewDidLoad或viewDidAppear中检查设备方向

    if( [UIDevice currentDevice].orientation == UIInterfaceOrientationLandscapeLeft)
OR if(UIInterfaceOrientationIsLandscape([UIDevice currentDevice].orientation))
Run Code Online (Sandbox Code Playgroud)

因为很多时候它会产生不可预期的结果,就像iPhone/iPad放在桌子上一样(我经历过这个......非常讨厌的问题).

Apple开发人员链接将提供有关处理方向的更多信息