我正在尝试制作一个基于TabBarController的应用程序,它在选项卡栏的中间有一个自定义按钮,就像Instagram和其他类似的应用程序一样。
我知道它将是一个自定义按钮的原因是,当用户按下按钮时,他/她不应该期望看到一个“视图控制器”分段行为,而是一个呈现的视图控制器。就我的研究而言,除了切换到选项卡栏所拥有的另一个视图控制器之外,很难定制选项卡栏项目来做任何事情。
假设我是对的,按钮必须是自定义的(如果你知道如何在标签栏上添加一个栏按钮项,并且让它呈现一个vc而不是切换到vc,请在你的答案中也包括它),我在标签栏控制器的视图中添加了一个自定义按钮:
- (void)addPostButton {
button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, 55, 55);
[button setImage:[UIImage imageNamed:@"post.png"] forState:UIControlStateNormal];
[button setBackgroundColor:[UIColor colorWithRed:32.0/255.0 green:35.0/255.0 blue:44.0/255.0 alpha:1]];
[button.layer setCornerRadius:button.frame.size.height / 2];
[button.layer setBorderColor:[UIColor whiteColor].CGColor];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:@selector(addPost) forControlEvents:UIControlEventTouchUpInside];
CGFloat heightDifference = 60 - self.tabBar.frame.size.height;
if (heightDifference < 0)
button.center = self.tabBar.center;
else
{
CGPoint center = self.tabBar.center;
center.y = center.y - heightDifference/2.0-5;
button.center = center;
}
[self.tabBar addSubview:button];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self addPostButton];
}我的故事板当前构造为:--root-->标签栏控制器--视图控制器-->导航控制器--根-->视图控制器。现在,我正在尝试让导航控制器推送一个视图,并希望在推送的视图中隐藏标签栏。我检查了“在推送时隐藏底部栏”,它工作得很好,除了添加的子视图仍然在那里。
显然,'hidesBottomBarOnPush‘只隐藏了tabbarcontroller.tabbar,而不是tabbarcontroller.view持有的其他子视图。
我还尝试将自定义按钮添加到tabbarcontroller.tabbar中,但没有显示任何内容。因此,当我弹出刚按下的vc时,我似乎必须将按钮添加到它的视图中,而不是它的选项卡bar.And,添加的按钮在选项卡栏的下面,而不是它的正上方。the栏控制器的视图层次结构有问题。
如果您知道任何解决方案,可以向tabbarcontroller.tabbar添加一个自定义按钮,或者以一种优雅的方式隐藏tabbarControler.view的子视图,我将非常感谢。
发布于 2017-06-30 03:00:44
这很可能是因为您向选项卡栏控制器添加了子视图,而不是选项卡栏本身。有几种方法可以解决这个问题,这取决于您添加按钮的方式。
https://stackoverflow.com/questions/23456109
复制相似问题