【发布时间】:2013-06-30 09:35:33
【问题描述】:
我需要为纵向和横向使用不同的 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;
}
在我的视图控制器中:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
return (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown);
}
在我的委托中。
我有两个可能相关的问题。第一,简单的。我不倒转。我在 iPad 和 iPhone 的 xcode 中打开了所有正确的位。这可能是一个单独的问题,也可能是我的问题的核心。
真正的问题是,当我旋转到横向模式时,我的 xib 被替换,但视图偏离了 90 度。
这是我的 2 xib 的样子。 (我给它们涂上了花哨的颜色,所以你可以看到它们是不同的。)
和
当我运行它(最初在横向模式下)时,您可以看到横向 xib 是正确的。
当我旋转到纵向时它也是正确的
但是当我旋转回横向时,xib 被替换,但视图偏离了 90 度。
这里有什么问题?
【问题讨论】:
-
我认为您将 xib 加载到早期,您是否尝试将代码从
willRotatetToInterface..移动到didRotateFrom...? -
hmmmm,这给了我从方向。由于我的应用程序基本上只有 2 个可能的应用程序,我想这没问题,但它似乎不干净......
-
它也不起作用。洋红色现在是全宽的,但 UI 小部件仍然偏离 90 度。
-
嗨,你找到解决问题的方法了吗,我目前也面临同样的问题,我发现的唯一方法是删除第二个 xib,并在代码中进行所有框架更改.. . :*(
-
我遇到了同样的问题,但很难相信有这么多人说这对他们来说是正确的。是什么赋予了?在视图切换设计方面,我真的不想走得那么远。
标签: ios ios6 autorotate