gpt4 book ai didi

ios - 在运行时更改方向更改 ViewControllers

转载 作者:行者123 更新时间:2023-11-29 03:26:02 25 4
gpt4 key购买 nike

我想更改 ViewControllers On Screen 方向更改

我只有一个 ViewController 类和两个 StoryboardBased UIViewController。

  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 文件中加载不同的 Controller ?

最佳答案

你可以这样做

   - (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];
}
}

希望这对你有帮助......

关于ios - 在运行时更改方向更改 ViewControllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20452926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com