我遇到一种情况,我创建了两个不同的 Nib ,一个处于纵向模式,另一个处于横向模式。我的 View 中有很多设计,所以我不得不选择两种不同的 Nib 。现在,我想在界面旋转时切换 Nib
common viewController
这样我就可以保留 View 中的填充值和控件的状态。
现在我正在使用
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Override to allow orientations other than the default portrait orientation.
if(interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ){
[self initWithNibName:@"LandscapeNib" bundle:nil];
}else if(interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
[self initWithNibName:@"PortraitNib" bundle:nil];
}
return YES;
}
但它不会改变 Nib ,它显示初始加载的 Nib 。我猜它正在加载但没有显示,因为初始 Nib 已经显示,没有被删除。我无法找到使用通用 View Controller 使用多个 Nib 的解决方案,以便我可以轻松处理控件的功能?
最佳答案
您应该为横向模式创建一个特殊的 View Controller ,并在设备旋转时以模态方式呈现它。要在设备旋转时收到通知,请注册 UIDeviceOrientationDidChangeNotification
通知。
对于需要以纵向和横向不同方式呈现的复杂 View ,这是 Apple 推荐的实现方式。阅读此处了解更多详细信息:
这是 Apple 文档的摘录。在 init 或 awakeFromNib 中注册通知:
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
并在orientationChanged中以模态方式呈现您的横向 View Controller ,或将其关闭,所有这些都根据当前旋转:
- (void)orientationChanged:(NSNotification *)notification
{
UIDeviceOrientation deviceOrientation = [UIDevice currentDevice].orientation;
if (UIDeviceOrientationIsLandscape(deviceOrientation) &&
!isShowingLandscapeView)
{
[self presentModalViewController:self.landscapeViewController
animated:YES];
isShowingLandscapeView = YES;
}
else if (deviceOrientation == UIDeviceOrientationPortrait &&
isShowingLandscapeView)
{
[self dismissModalViewControllerAnimated:YES];
isShowingLandscapeView = NO;
}
}
关于iphone - 想要针对不同的 iPhone 界面方向使用多个 Nib ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3786727/