Ios 横向xib显示为纵向
我有一个视图控制器和用于纵向和横向的独立nib文件。旋转时,我加载相应的笔尖。方法Ios 横向xib显示为纵向,ios,rotation,Ios,Rotation,我有一个视图控制器和用于纵向和横向的独立nib文件。旋转时,我加载相应的笔尖。方法 shouldAutorotateToInterfaceOrientation and willRotateToInterfaceOrientation 接到电话后,笔尖会发生变化 问题是: the landscape nib does not appear as landscape, but portrait! The status bar is correctly rotated and appears o
shouldAutorotateToInterfaceOrientation and willRotateToInterfaceOrientation
接到电话后,笔尖会发生变化
问题是:
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中显示为横向(如果视图是纵向的,我甚至不认为可以创建这样的旋转按钮)。除了这两个笔尖之外,我还有一个主窗口。xib,其中包含应用程序委托、窗口和视图控制器对象
编辑:我意识到视图实际上是在旋转,而它们应该“保持不动”。这就像有一个额外的转变。当我将手机向右旋转90°时,横向xib显示为向右旋转90°。当我将手机再向右旋转90°时,画像xib会颠倒显示。状态栏始终正确显示在顶部
编辑2:使用
self.view.transform = CGAffineTransformMakeRotation((M_PI * (90) / 180.0));
在willRotateToInterfaceOrientation中,我可以将视图旋转到横向左侧(以及我想要的任何方向),因此我可以将其用作解决方法。但是,我还有其他项目,其中视图自动旋转,不需要使用CGAffineTransformMakeRotation。好像有什么东西阻止了这里的自动旋转。是否将从nib加载的
视图添加为子视图?如果只有状态栏在旋转,则表示在释放视图和添加新视图时,您的上一个视图处于挂起状态。您能告诉我如何将从xib
加载的视图添加到SuperView中吗
确保在加载另一个视图时正确释放上一个视图,将NSLOG
放入视图的dealloc中,检查视图是否完全释放。我做了类似的事情,只是没有单独创建nib文件,而是在主nib中添加了两个子视图,即prtraitView和横向视图
然后按如下方式切换它们
- (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];
}
}
}
在视图中显示方法
-(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];
}
然后实现DeviceOrientationIDChangeNotification,如下所示
- (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];
}
}
}
这对我来说非常有效。在你的景观xib中,你是否将方向设置为景观?如果是,将其更改为“纵向”。我已将其设置为“横向”。是的,我试着把它设置成肖像,但它仍然是一样的!在shouldAutorotateToInterfaceOrientation
中,返回YES代码>此外,在项目目标->摘要中,确保已设置支持的方向确保。如果不支持横向,状态栏将不会旋转。这没有任何区别。我使用ARC时不会释放状态栏。在视图控制器的willRotateToInterfaceOrientation中,我正在加载新的nib,如:UINib*nib=[UINib nibWithNibName:@“landscapetest”bundle:nil];UIView*landscapeView=[[nib实例化所有者:自选项:nil]对象索引:0];self.view=景观视图;谢谢,我稍后再试试。看起来很有希望。(但对于不需要额外工作的问题,这仍然是一个解决办法。)