在横向和纵向视图中旋转的问题

问题描述:

在我的应用程序中,我有2个视图,portraitView和landScapeView。而且我的应用程序还包括许多视图...在横向和纵向视图中旋转的问题

  1. 当我启动应用程序时,横向和纵向视图并排显示。

  2. 当我在landscapeview中启动应用程序时,portraitview正在显示..later旋转后返回视图正在变得适当。

我使用下面给出的代码......所以,请建议我什么都改变应该做提到克服以上问题..

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 

{ 

if (interfaceOrientation == UIInterfaceOrientationLandscapeLeft) { 
    NSLog(@"LANDSCAPE>>"); 
    portraitView.hidden = YES; 
    landscapeView.hidden = NO; 

    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic_landscape.jpg"]]; 
    self.view.backgroundColor = background; 



} 
else if (interfaceOrientation == UIInterfaceOrientationLandscapeRight){ 
    NSLog(@"LANDSCAPE<<"); 
    portraitView.hidden = YES; 
    landscapeView.hidden = NO; 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic_landscape.jpg"]]; 
    self.view.backgroundColor = background; 

    self.view.hidden = NO; 

} 
else{ 
    portraitView.hidden = NO; 
    landscapeView.hidden = YES; 


    NSLog(@"Portrait"); 
    UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic.jpg"]]; 
    self.view.backgroundColor = background; 

    background = [[UIColor alloc] initWithPatternImage:[UIImage imageNamed:@"bg-ic2.jpg"]]; 
    self.view.backgroundColor = background; 

} 

return YES; 
+0

为什么要为风景和肖像制作不同的视图?为什么你没有相应地根据你所处的方位来固定框架尺寸? –

+0

要更清楚您只需要使用一个视图。以编程方式或通过适合的界面构建器设置视图的框架。 – iamsult

听起来像你的portraitView和landscapeView都在应用程序的初始启动时可见。上述代码仅在定位在运行时更改时执行。如果您坚持使用单独的视图进行纵向和横向视图,那么您还需要在启动时检测方向,并适当地显示/隐藏视图。

,系统调用shouldAutorotateToInterfaceOrientation事实:并不意味着它会在那里那么做。您想要使用willRotateToInterfaceOrientation和didRotateFromInterfaceOrientation,并尽可能使用自动调整大小。您也可以继承布局更新回调以重新排列视图。不建议使用两个视图并将它们显示/隐藏为横向/纵向。