Edit :我给了约翰的赏金,因为他在他的答案中付出了很多努力,并且无论如何都会得到它,但是我会非常感激 .
我想在我的应用程序中添加一个“最大化”按钮,隐藏导航和标签栏 . 导航栏和标签栏应平滑地滑入/滑出,内部/内容视图也应以与导航栏和标签栏相同的速率展开和缩小 .
我使用 [self.navigationController setNavigationBarHidden: YES/NO animated: YES];
作为导航栏,发现此线程How to hide uitabbarcontroller用于隐藏标签栏 .
UITabBar类扩展:
- (void) setTabBarHidden:(BOOL)hidden animated:(BOOL)animated {
CGRect screenRect = [[UIScreen mainScreen] bounds];
float screenHeight = screenRect.size.height;
if (UIDeviceOrientationIsLandscape([UIDevice currentDevice].orientation)) {
screenHeight = screenRect.size.width;
}
if (!hidden) {
screenHeight -= self.tabBar.frame.size.height;
}
[UIView animateWithDuration: (animated ? UINavigationControllerHideShowBarDuration : 0) animations: ^{
for (UIView* each in self.view.subviews) {
if (each == self.tabBar) {
[each setFrame: CGRectMake(each.frame.origin.x, screenHeight, each.frame.size.width, each.frame.size.height)];
} else {
[each setFrame: CGRectMake(each.frame.origin.x, each.frame.origin.y, each.frame.size.width, screenHeight)];
}
}
} completion: ^(BOOL finished) {
NSLog(@"Animation finished %d", finished);
}];
}
问题是当我同时使用这两个(隐藏/显示导航和标签栏)时,它不干净 . 如果导航栏首先出现,锚定到底部的任何东西都会跳跃(参见下面的示例),如果标签栏首先出现,则顶部跳跃 .
示例:我将UIButton置于右下角并设置其自动调整遮罩
resizeButton.frame = CGRectMake(self.view.bounds.size.width - 50, self.view.bounds.size.height - 100, 32, 32); // hardcoded just for testing purposes
resizeButton.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
但是当导航栏和标签栏最小化时,UIButton会在两种状态之间跳转(不会与标签栏一起滑动) . 但是,如果我将其更改为附加到右上角,则它会与导航栏完美滑动 .
有谁知道如何解决这个问题?
Edit :这是迄今为止我所拥有的壁橱和最优雅的解决方案(只是试图获得一个有效的概念):
[UIView animateWithDuration: UINavigationControllerHideShowBarDuration animations: ^{
if (self.isMaximized) {
self.tabBarController.view.frame = CGRectMake(0, 20, screenRect.size.width, screenRect.size.height + 49 - 20);
[self.navigationController setNavigationBarHidden:YES animated:YES];
} else {
self.tabBarController.view.frame = CGRectMake(0, 20, screenRect.size.width, screenRect.size.height - 20);
[self.navigationController setNavigationBarHidden:NO animated:YES];
}
} completion: ^(BOOL finished) {
NSLog(@"Frame done: %@", NSStringFromCGRect(self.view.frame));
return;
}];
最大化:
-
向上滑动导航栏,同时向下滑动标签栏
-
内部/内容视图的顶部向上滑动,此视图的底部向下跳跃
最小化:
-
向下滑动导航栏,同时向上滑动标签栏
-
内部/内容视图的顶部正确向下滑动,但底部跳转到最终值,留下空白,然后由滑动标签栏覆盖
如果我重新排列最小化动画的顺序(因此首先调用navbar animatino),那么内部/内容视图中的顶部会跳转
3 回答
我使用的解决方案应该消除你看到的跳转问题 .
这个解决方案来自于找到Carlos Oliva's github page的Objective-C类别,虽然该代码的版权是"all rights reserved",但我写了他并且他提供了使用许可 .
我的类别代码与他的代码略有不同 . 另外,在类别代码下面找到我在我的应用程序中使用的调用代码 .
来自UITabBarController HideTabBar.m
来自我的ScrollableDetailImageViewController.m
Just try this code, If it is working. I have written this code a year before. But, still it works good for me.
I didn't used the block based animations. Because it was written when I am new to iOS. Just try and optimize yourself as you wanted.
Try this
您可以使用以下动画隐藏tabbar控制器和导航栏: -