gpt4 book ai didi

ipad - 使用一个UIViewController和两个XIB在iPad上处理方向更改

转载 作者:行者123 更新时间:2023-12-04 03:34:41 26 4
gpt4 key购买 nike

我想用一个UIViewController和两个XIB处理iPad应用程序上的方向更改,比如说MenuView和MenuViewLandscape。

因此,在MenuViewController的willRotateToInterfaceOrientation方法中,如何在不将另一个 Controller 用于横向模式的情况下更改XIB?

我正在使用以下代码:

if( toInterfaceOrientation != UIInterfaceOrientationPortrait ){
MenuViewController *landscape = [[MenuViewController alloc]
initWithNibName: @"MenuViewLandscape"
bundle:nil
];
[self setView:landscape.view];
}
else {
MenuViewController *potrait = [[MenuViewController alloc]
initWithNibName: @"MenuView"
bundle:nil
];
[self setView:potrait.view];
}

但是,当我转到景观 View XIB时,景观 View 控件未正确旋转。

最佳答案

我不确定此实现有任何奇怪的副作用,但是请尝试这样的操作,看看它是否对您有用:

-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)orientation duration:(NSTimeInterval)duration {
if (UIInterfaceOrientationIsPortrait(orientation)) {
[[NSBundle mainBundle] loadNibNamed:@"MenuView" owner:self options:nil];
if (orientation == UIInterfaceOrientationPortraitUpsideDown) {
self.view.transform = CGAffineTransformMakeRotation(M_PI);
}
} else if (UIInterfaceOrientationIsLandscape(orientation)){
[[NSBundle mainBundle] loadNibNamed:@"MenuViewLandscape" owner:self options:nil];
if (orientation == UIInterfaceOrientationLandscapeLeft) {
self.view.transform = CGAffineTransformMakeRotation(M_PI + M_PI_2);
} else {
self.view.transform = CGAffineTransformMakeRotation(M_PI_2);
}
}
}

假设您的MenuView和MenuViewLandscape XIB中的文件所有者都设置为MenuViewController,并且两个XIB中都设置了 View 导出。使用 loadNibNamed时,应在旋转时正确重新连接所有 socket 。

如果要针对iOS 4进行构建,则还可以将 loadNibNamed行替换为以下内容:
UINib *nib = [UINib nibWithNibName:@"MenuView" bundle:nil];
UIView *portraitView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = portraitView;


UINib *nib = [UINib nibWithNibName:@"MenuViewLandscape" bundle:nil];
UIView *landscapeView = [[nib instantiateWithOwner:self options:nil] objectAtIndex:0];
self.view = landscapeView;

这些假定您要显示的UIView紧随XIB中的“文件的所有者”和“第一响应者”代理对象。

然后,您只需要确保 View 已针对界面方向正确旋转即可。对于所有不是以默认纵向显示的 View ,通过设置 View 的 transform属性并使用具有适当值的 CGAffineTransformMakeRotation()来旋转它们,如上面的示例所示。

单独轮换可能会解决您的问题,而不会增加NIB的负担。但是,加载一个 MenuViewController的全新实例并将其 View 设置为现有 MenuViewController的 View 可能会导致生命周期和旋转事件发生某些奇怪的行为,因此您可以更安全地尝试上述示例。当您只需要它的 View 时,它们还为您省去了创建新的 MenuViewController实例的麻烦。

希望这可以帮助!

贾斯汀

关于ipad - 使用一个UIViewController和两个XIB在iPad上处理方向更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4222701/

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