Ios 在运行时更改方向时更改ViewController
我想更改屏幕上的ViewController方向更改 我只有一个类ViewController和两个基于故事板的UIViewControllerIos 在运行时更改方向时更改ViewController,ios,objective-c,ipad,cocoa-touch,Ios,Objective C,Ipad,Cocoa Touch,我想更改屏幕上的ViewController方向更改 我只有一个类ViewController和两个基于故事板的UIViewController UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideD
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation;
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown)
{
[[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
}
else {
[[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
}
该代码可用于在方向改变时加载不同的xib。但是,在方向更改时,如何在Xib文件中加载不同的控制器?如果采用这种方法,则不会在Xib中加载不同的视图控制器。一个xib表示一个视图及其子视图 然而,我会遵循另一种方法。我将创建一个视图a(“主机”视图控制器的self.view),它只有两个全屏容器视图,一个用于横向视图,一个用于纵向视图。 只需将两者都自动调整大小,以便它们始终占据整个屏幕 在旋转的伊夫茨,我只能相应地隐藏和解开它们
但为什么您认为每次旋转都需要单独的视图控制器 这里最好的方法可能是使用。您应该做的是使用一个包装器视图控制器,它嵌入来自两个视图控制器的两个视图(每个方向一个),并使用iOS API提供的方法在视图之间切换
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
-(void)orientationChanged:(NSNotification *)notification
{
UIInterfaceOrientation toInterfaceOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (UIInterfaceOrientationIsPortrait(toInterfaceOrientation))
{
[[NSBundle mainBundle] loadNibNamed:@"Portrait" owner:self options:nil];
}
else if (UIInterfaceOrientationIsLandscape(toInterfaceOrientation))
{
[[NSBundle mainBundle] loadNibNamed:@"Landscape" owner:self options:nil];
}
}
希望这能帮助你……动画怎么样,看起来会不会很奇怪?你可以为隐藏和取消隐藏设置动画。你试过了吗?