我将此代码用作我的标签栏类,它为我提供了一个带有更大中心按钮的自定义标签栏。我的问题是当我尝试隐藏标签栏时,标签栏消失了,但中心按钮图像仍然可见。如何使用标签栏隐藏中间按钮?
[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 按钮是隐藏的。