我需要为纵向和横向使用不同的 xib 文件。我没有使用自动布局,但我使用的是 iOS6。 (如果您关心原因,请参阅 my previous question。)
我正在关注 Adam 对 this question 的回答使用 amergin 的 initWithNib 名称技巧进行修改,根据我自己的 iPhone/iPad 需求进行修改。这是我的代码:
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[[NSBundle mainBundle] loadNibNamed:[self xibNameForDeviceAndRotation:toInterfaceOrientation]
owner: self
options: nil];
[self viewDidLoad];
}
- (NSString *) xibNameForDeviceAndRotation:(UIInterfaceOrientation)toInterfaceOrientation
{
NSString *xibName ;
NSString *deviceName ;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
deviceName = @"iPad";
} else {
deviceName = @"iPhone";
}
if( UIInterfaceOrientationIsLandscape(toInterfaceOrientation) )
{
xibName = [NSString stringWithFormat:@"%@-Landscape", NSStringFromClass([self class])];
if([[NSBundle mainBundle] pathForResource:xibName ofType:@"nib"] != nil) {
return xibName;
} else {
xibName = [NSString stringWithFormat:@"%@_%@-Landscape", NSStringFromClass([self class]), deviceName];
if([[NSBundle mainBundle] pathForResource:xibName ofType:@"nib"] != nil) {
return xibName;
} else {
NSAssert(FALSE, @"Missing xib");
return nil;
}
}
} else {
xibName = [NSString stringWithFormat:@"%@", NSStringFromClass([self class])];
if([[NSBundle mainBundle] pathForResource:xibName ofType:@"nib"] != nil) {
return xibName;
} else {
xibName = [NSString stringWithFormat:@"%@_%@", NSStringFromClass([self class]), deviceName];
if([[NSBundle mainBundle] pathForResource:xibName ofType:@"nib"] != nil) {
return xibName;
} else {
NSAssert(FALSE, @"Missing xib");
return nil;
}
}
}
}
当然我在做:
- (BOOL) shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations {
return UIInterfaceOrientationMaskAll;
}
在我的 View Controller 中:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
在我的委托(delegate)中。
我有两个可能相关的问题。第一,简单的。我不会倒转。我在 xcode 中为 iPad 和 iPhone 打开了所有正确的位。这可能是一个单独的问题,也可能是我的问题的核心。
真正的问题是,当我旋转到横向模式时,我的 xib 被替换,但 View 偏离了 90 度。
这是我的 2 个 xib 的样子。 (我给它们涂上了鲜艳的颜色,这样你就可以看出它们是不同的。)
和
当我运行它时(最初在横向模式下)你可以看到横向 xib 是正确的。
当我旋转到纵向时也是正确的
但是当我旋转回横向时,xib 被替换,但 View 偏离了 90 度。
这里有什么问题吗?
最佳答案
我一直在走与 Paul Cezanne 去年走的路可能相同的路。不确定他是否尝试过这个,但我通过将我的根 Controller 设为导航 Controller 而不是我的 View Controller 类来解决了原始问题(在这个问题中陈述)。由于我使用的是“空项目”模板和 XIB 文件,这意味着要改变常规:
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
在 AppDelegate.m 中,改为:
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navigationController.navigationBar.hidden = YES;
self.window.rootViewController = navigationController;
也就是说,我刚刚创建了一个通用的 UINavigationController 并将其设置为 Root View Controller 。
我不确定这是否会导致其他问题,并且可能有一种方法可以弄清楚(尽管您可能需要源代码)UINavigationController 做了什么而 UIViewController 没有。可以像在正确位置调用一个额外的 setNeedsLayout 类型一样简单。如果我弄清楚了,我会为 future 的读者编辑这个答案。
感谢 Sakti 对 Easiest way to support multiple orientations? How do I load a custom NIB when the application is in Landscape? 的评论我在第一次阅读它们时不应该忽略这一点:
i added the view controller to navigation controller and presented it which made it work as intended
编辑:在示例代码中添加了额外的行以隐藏导航栏,因为关注此问题的大多数人不希望这样做。
关于ios - 在 iOS6 下使用多个 xibs 进行轮换的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17435397/