无法在标签栏内隐藏中心图像
Posted
技术标签:
【中文标题】无法在标签栏内隐藏中心图像【英文标题】:Can't hide center image inside a tab bar 【发布时间】:2012-12-17 19:52:59 【问题描述】:我将此代码用作我的标签栏类,它为我提供了一个带有更大中心按钮的自定义标签栏。我的问题是当我尝试隐藏标签栏时,标签栏消失了,但中心按钮图像仍然可见。如何用标签栏隐藏中间按钮?
[self addCenterButtonWithImage:[UIImage imageNamed:@"hood.png"] highlightImage:[UIImage imageNamed:@"hood-selected.png"] target:self action:@selector(buttonPressed:)];
....
- (void)addCenterButtonWithImage:(UIImage *)buttonImage highlightImage:(UIImage *)highlightImage target:(id)target action:(SEL)action
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
button.autoresizingMask = UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleTopMargin;
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:highlightImage forState:UIControlStateHighlighted];
CGFloat heightDifference = buttonImage.size.height - 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;
button.center = center;
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
更新:
我写的另一个班级:
TabBarController *blah = [[WBTabBarController alloc]init];
[blah hideButton];
....
TabBarController.h:
@property (nonatomic, strong) UIButton* button;
TabBarController.m:
self.button = button;
...
-(void)hideButton
_button.hidden = YES;
NSLog(@"Test!!!");
但它不起作用。如果我将_button.hidden = YES;
放在 TabBarController 的 viewDidLoad 中,按钮就会被隐藏。
【问题讨论】:
【参考方案1】:[self.button removeFromSuperView]
或 button.hidden=YES
当您像这里一样添加子视图时 [self.view addSubview:button];然后你想删除它,你通常会这样做 [self.button removeFromSuperView];
在隐藏标签栏的方法中放置 [self.button removeFromSuperView];这也行不通吗?
【讨论】:
这给了我一个方向。还是没有解决,我已经更新了第一个帖子以上是关于无法在标签栏内隐藏中心图像的主要内容,如果未能解决你的问题,请参考以下文章