我需要为纵向和横向使用不同的xib文件。我没有使用自动布局,但我使用的是iOS6。 (见my previous question如果你在乎为什么。)在iOS6下旋转多个xibs的问题
我跟随亚当的回答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;
}
在我的视图控制器
和:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
在我的委托。
我有两个可能相关的问题。首先,简单的一个。我不旋转颠倒。我已经在xcode中为iPad和iPhone开启了所有适当的位。这可能是一个单独的问题,也可能是我的问题的核心。
真正的问题是,当我旋转到横向模式时,我的xib被替换,但视图关闭了90度。
这是我的2 xib的样子。 (我已经有色他们华丽地,所以你可以看到,他们是不同的。)
和
,你可以看到,当我(在风景模式最初)运行它的景观厦门国际银行是正确的。
当我旋转为纵向,也正确
但是当我转回到景观厦门国际银行被替换,但认为是关闭的90度。
这里有什么错?
我想你是加载到早期的xib,你是否尝试将代码从'willRotatetToInterface..'移动到'didRotateFrom ...'中? – danypata
嗯,这给了我从方向。由于基本上只有2个可能的我的应用程序,我猜这是好的,但它似乎不干净...... –
,它也行不通。洋红色现在是全宽,但UI小部件仍然偏离90度。 –