横屏的xib显示为竖屏。

10

我有一个视图控制器,还有独立的横屏和竖屏nib文件。在旋转时,我会加载相应的nib文件。相应的方法如下:

 shouldAutorotateToInterfaceOrientation and willRotateToInterfaceOrientation

get called and the nib does change.

The problem:

the landscape nib does not appear as landscape, but portrait! The status bar is
correctly rotated and appears on the top:
(Sorry, couldn't paste the image, because my account is new. The screenshot is in   
landscape, with a landscape status bar, but a landscape view shown as portrait.)

有人可能认为问题在于未在IB模拟度量中将方向设置为横向,但我已经这样做了,在IB中该视图显示为横向。(如果视图是纵向的,甚至不可能创建旋转的按钮。)除了这两个nib文件外,我还有一个 mainwindow.xib ,其中包含应用程序委托、窗口和视图控制器对象。

编辑:我意识到视图实际上正在旋转,而它们应该“保持上升”。就像有一个额外的变换一样。当我将手机向右旋转90°时,横向xib显示为向右旋转90°。当我再次将手机向右旋转90°时,纵向xib显示为倒置。状态栏始终正确地显示在顶部。

编辑2:使用

self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));

在willRotateToInterfaceOrientation方法中,我可以将视图旋转到左横屏(以及任何我想要的方向),因此我可以将其用作解决方法。但是,我还有其他项目,其中视图会自动旋转,不需要使用CGAffineTransformMakeRotation。就好像这里有什么东西阻止了自动旋转。

在你的 landscape xib 中,你是否将方向设置为 landscape?如果是,请将其更改为 portrait。 - tipycalFlow
我已将其设置为横向模式。是的,我尝试过将其设置为纵向模式,但它仍然以相同的方式显示! - james0n
shouldAutorotateToInterfaceOrientation 方法中,返回 YES;。此外,在您的项目 Targets->Summary 中,请确保您已设置支持的方向。 - tipycalFlow
当横向方向不被支持时,状态栏将不会旋转。 - james0n
1
这没有任何区别。 - james0n
显示剩余3条评论
2个回答

0

我之前做过类似的事情,只不过没有单独制作一个nib文件,而是将两个子视图添加到主nib中,分别作为竖屏视图和横屏视图。

然后按以下方式进行切换:

在viewDidAppear方法中。

-(void)viewDidAppear:(BOOL)animated{




    if(UIDeviceOrientationIsPortrait(self.interfaceOrientation))
    {
        self.portraitVIew.frame=self.view.bounds;
        self.portraitVIew.frame=self.view.frame;
        [self.view addSubview:self.portraitVIew];
    }else{
        self.landscapeView.frame=self.view.frame;
        [self.view addSubview:self.landscapeView];
    }
    self.view.autoresizesSubviews = YES;




    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(deviceOrientationDidChangeNotification:)
     name:UIDeviceOrientationDidChangeNotification
     object:nil];


}

然后按如下所示实现了deviceOrientationDidChangeNotification

- (void)deviceOrientationDidChangeNotification:(NSNotification*)note
{

    if ([[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad) {

    }else{
        UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
        if(UIDeviceOrientationIsLandscape(self.interfaceOrientation))
        {
            self.landscapeView.hidden=NO;
            self.landscapeView.frame=self.view.frame;
            [self.portraitVIew removeFromSuperview];


            [self.view addSubview:self.landscapeView];
        }else {
            self.landscapeView.hidden=YES;
            self.portraitVIew.frame=self.view.frame;
            NSLog(@"Portrait");

            [self.view addSubview:self.portraitVIew];
        }
    }



}

这对我非常有效。


谢谢,我稍后会尝试。看起来很有前途。(但仍然是一个解决问题的变通方法,本不需要额外的工作。) - james0n

0
你是否将从nib加载的view添加为子视图?如果只有状态栏在旋转,这意味着你的之前的视图已经挂起,同时释放了该视图并添加了新的视图。你能告诉我你是如何将从xib加载的视图添加到SuperView中的吗?
确保在加载其他视图时正确释放之前的视图,在视图的dealloc方法中放置NSLOG,并检查视图是否完全释放。

我不进行释放,因为我正在使用ARC。在视图控制器的willRotateToInterfaceOrientation中,我像这样加载新的nib: UINib *nib = [UINib nibWithNibName:@"landscapetest" bundle:nil]; UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0]; self.view = landscapeView; - james0n

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接