所以这就是我所拥有的: 一个处理不同UIViewControllers的UITabBarController。在其中一个UIViewController中,我试图在设备旋转到横向时切换显示的视图。 重要的是,景观中显示的视图必须占据整个屏幕...
我已正确实施这些方法:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
事实上,我确实正确地进行了轮换,而且我的观点也在变化。 我甚至隐藏状态栏,导航栏和标签栏但我在屏幕底部仍然有一个空白区域,这是TabBar的位置......
所以我假设设置tabBar的隐藏属性是不够的,以便在整个屏幕上显示视图。我认为在TabBarController甚至MainWindow中都有一些事情可以说“我现在不需要TabBarController”。但我没有看到如何妥善解决这个问题。
如果有人围绕这个问题,我将不胜感激。
谢谢你, 萨米。答案 0 :(得分:33)
这对我有用。
- (void)viewDidLoad {
[super viewDidLoad];
previousRect = self.view.frame;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration;
{
if(toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft || toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
[self.navigationController setNavigationBarHidden:TRUE animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:TRUE animated:FALSE];
}
else
{
[self.navigationController setNavigationBarHidden:FALSE animated:FALSE];
[[UIApplication sharedApplication] setStatusBarHidden:FALSE animated:FALSE];
}
}
-(void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation {
UIInterfaceOrientation toOrientation = self.interfaceOrientation;
if ( self.tabBarController.view.subviews.count >= 2 )
{
UIView *transView = [self.tabBarController.view.subviews objectAtIndex:0];
UIView *tabBar = [self.tabBarController.view.subviews objectAtIndex:1];
if(toOrientation == UIInterfaceOrientationLandscapeLeft || toOrientation == UIInterfaceOrientationLandscapeRight) {
transView.frame = CGRectMake(0, 0, 480, 320 );
tabBar.hidden = TRUE;
}
else
{
transView.frame = previousRect;
tabBar.hidden = FALSE;
}
}
}
答案 1 :(得分:1)
答案 2 :(得分:0)
此代码工作正常,但是当我关闭以模态方式呈现的uiviewcontroller时,我的视图位于状态栏下20像素。 我的视图位于导航控制器内部,所以我不会在旋转之前隐藏它。
答案 3 :(得分:0)
我需要标签栏在横向视图中进入全屏模式,我尝试使用
建议上面的方法 transView.frame = CGRectMake(0, 0, 480, 320 );
这被证明是一个hacky解决方案并且提出了许多问题,例如隐藏和重新显示状态栏(当退出纵向视图后,视图将在重新显示时与状态栏重叠)。我不会推荐这个。最终对我有用的是推动一个包含横向视图的新视图控制器,并使用委托重用原始VC的功能。
答案 4 :(得分:0)
这种方法对我有用:
- (void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
UIView *parentView = self.tabBarController.view;
CGRect frame = parentView.frame;
CGFloat windowHeight = parentView.window.frame.size.height;
switch (toInterfaceOrientation) {
case UIInterfaceOrientationLandscapeLeft:
case UIInterfaceOrientationLandscapeRight:
CGFloat tabBarHeight = self.tabBarController.tabBar.frame.size.height;
frame.size.height = windowHeight + tabBarHeight;
break;
default:
frame.size.height = windowHeight;
break;
}
[UIView animateWithDuration:duration animations:^{
parentView.frame = frame;
}];
}
(仅在iOS8中测试过。)
答案 5 :(得分:0)
对TabBarController进行子类化并在需要时隐藏TabBar:
class tabBarVC: UITabBarController {
override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
if size.height < size.width {
self.tabBar.hidden = true
} else {
self.tabBar.hidden = false
}
}
}
答案 6 :(得分:0)
也许你想用这个
- (void)willAnimateRotationToInterfaceOrientation:UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
__block UIView *weakTabBar = [self.tabBarController.view.subviews objectAtIndex:1];
weakTabBar.alpha = 0;
[UIView animateWithDuration:duration
delay:0
options:UIViewAnimationOptionCurveEaseIn // slow at the beggining
animations:^{
weakTabBar.alpha = 1;
}
completion:^(BOOL finished) {
weakTabBar.alpha = 1;
}];
}
}
这并不会隐藏标签栏,但会使旋转动画更加流畅。
答案 7 :(得分:-1)
如果你有你的UITabBarController然后在其中放置一个UINavigationController,那么你可以使用hidesBottomBarWhenPushed(有点诡计)来做到这一点。
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[super willAnimateRotationToInterfaceOrientation:toInterfaceOrientation duration:duration];
if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
self.hidesBottomBarWhenPushed = NO;
self.navigationController.viewControllers = self.navigationController.viewControllers;
[self transitionToGridLayout];
}
else {
self.hidesBottomBarWhenPushed = YES;
self.navigationController.viewControllers = self.navigationController.viewControllers;
[self transitionToCoverflowLayout];
}
}
诀窍是推动你的视图控制器,以便拾取hidesBottomBarWhenPushed
标志。您可以使用以下内容。
self.navigationController.viewControllers = self.navigationController.viewControllers;