我使用下面的代码来实现它,但它不起作用。请给我一些建议。非常感谢。
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
orientationChanged() 函数:
- (void)orientationChanged:(NSNotification *)notification{
[self adjustViewsForOrientation:[[UIApplication sharedApplication] statusBarOrientation]];
}
adjustViewsForOrientation() 函数:
- (void) adjustViewsForOrientation:(UIInterfaceOrientation) orientation {
if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown)
{
NSLog(@"load portrait view");
[[NSBundle mainBundle] loadNibNamed:@"DashBoardViewController" owner:self options:nil];
}
else if (orientation == UIInterfaceOrientationLandscapeLeft || orientation == UIInterfaceOrientationLandscapeRight)
{
NSLog(@"load landscapeview");
[[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil];
}
}
运行时总是显示 DashBoardViewController.xib。在 lanscape 中,它仍然显示 DashBoardViewController.xib。我不知道为什么。
最佳答案
这是因为您实际上没有设置 View 。
你需要做类似的事情:
self.view = [[NSBundle mainBundle] loadNibNamed:@"DashBoardLandscapeView" owner:self options:nil] objectAtIndex:0];
这将从 Xib 文件加载 View ,并将当前 View 设置为该 View 。
不要忘记添加 objectAtIndex:0,如代码中所示。
关于ios - 在 iOS 中定位时加载另一个 .xib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20721389/