交互PopGesture时如何处理UITabBarController的子视图(如Flipboard)
Posted
技术标签:
【中文标题】交互PopGesture时如何处理UITabBarController的子视图(如Flipboard)【英文标题】:How to handle subviews of UITabBarController when interactivePopGesture (like Flipboard) 【发布时间】:2015-01-09 20:27:41 【问题描述】:我想要一个下划线来指示选择了哪个项目。每当点击该项目时,它就会滑动到任何其他项目。因此,我在自定义UITabBarController
中添加了一个子视图并设置了动画。然后我使用hidesBottomBarWhenPushed
在推送时隐藏标签栏。但是,下划线似乎没有与自定义 UITabBarController
结合使用。
如何处理子视图,使其即使在使用后退手势时也始终位于顶部?这个Flipboard应用捕获是我想做的。
编辑:
CustomTabBarController.m
- (void)viewDidLoad
[super viewDidLoad];
// create underline view
CGRect tabBarFrame = self.tabBar.frame;
CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarFrame) / MIN(5, self.tabBar.items.count);
CGFloat originX = (CGFloat)itemWidth * self.selectedIndex;
CGRect underlineFrame = CGRectMake(originX, CGRectGetMaxY(tabBarFrame) - 3.0f, itemWidth, 3.0f);
self.underlineView = [[UIView alloc] initWithFrame:underlineFrame];
self.underlineView.backgroundColor = [UIColor redColor];
[self.view addSubview:self.underlineView];
#pragma mark - UITabBarDelegate
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
NSUInteger itemIndex = [tabBar.items indexOfObject:item];
CGRect underlineFrame = self.underlineView.frame;
CGFloat originX = (CGFloat)CGRectGetWidth(self.underlineView.frame) * itemIndex;
// underline shifting animation
[UIView animateWithDuration:0.25
animations:^
self.underlineView.frame = CGRectMake(originX, underlineFrame.origin.y, CGRectGetWidth(underlineFrame), CGRectGetHeight(underlineFrame));
];
CustomTableViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
UIViewController *detailViewController = segue.destinationViewController;
detailViewController.hidesBottomBarWhenPushed = YES;
hidesBottomBarWhenPushed
隐藏标签栏,但隐藏其子视图(下划线视图)。
如果我自己隐藏它并在viewWillAppear
中显示它,下划线视图看起来不像在标签栏顶部。
【问题讨论】:
您应该首先展示您的一些代码,以便我们提供帮助。从图片中我看不出为什么你的可能不工作 【参考方案1】:我终于找到了解决方法。要覆盖 hidesBottomBarWhenPushed
方法,您可以为标签栏的子视图添加替代视图。
sourceViewController.m
- (BOOL)hidesBottomBarWhenPushed
[super hidesBottomBarWhenPushed];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
if (tabBarController.underlineView.isHidden)
CGRect tabBarBounds = tabBarController.tabBar.bounds;
CGFloat underlineHeight = CGRectGetHeight(tabBarController.underlineView.frame);
CGFloat itemWidth = (CGFloat)CGRectGetWidth(tabBarBounds) / MIN(5, tabBarController.tabBar.items.count);
CGFloat originX = (CGFloat)itemWidth * tabBarController.selectedIndex;
UIView *alternativeView = [[UIView alloc] initWithFrame:CGRectMake(originX,
CGRectGetMaxY(tabBarBounds) - underlineHeight,
itemWidth,
underlineHeight)];
alternativeView.tag = tabBarController.underlineViewTag;
alternativeView.backgroundColor = tabBarController.underlineView.backgroundColor;
[tabBarController.tabBar addSubview:alternativeView];
return NO;
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
if (tabBarController.underlineView.isHidden)
tabBarController.underlineView.hidden = NO;
NSInteger underlineViewTag = tabBarController.underlineViewTag;
UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
[alternativeView removeFromSuperview];
不要忘记interactivePopGesture
无法弹出视图控制器的情况,替代视图仍会添加到标签栏。因此,如果需要,请在目标视图控制器中将其删除。
destinationViewController.m
- (void)viewDidAppear:(BOOL)animated
[super viewDidAppear:animated];
CustomTabBarController *tabBarController = (CustomTabBarController *)self.tabBarController;
NSInteger underlineViewTag = tabBarController.underlineViewTag;
UIView *alternativeView = [tabBarController.tabBar viewWithTag:underlineViewTag];
if (alternativeView) [alternativeView removeFromSuperview];
【讨论】:
以上是关于交互PopGesture时如何处理UITabBarController的子视图(如Flipboard)的主要内容,如果未能解决你的问题,请参考以下文章
应用关闭时如何处理 UNNotificationAction?