隐藏自定义工具栏按钮
Hide Custom ToolBar Button
我创建了一个自定义标签栏并在中心位置添加了一个自定义图像。以下代码的作用是 expected.It 在标签栏的中心添加我的自定义按钮。
CustomTabBarViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* buttonImage = [UIImage imageNamed:@"icon_floating.png"];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
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:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) myAction
{
self.selectedIndex = 2;
}
我遇到的问题如下。有一个 viewcontroller - ChatViewContoller
我隐藏 tabbar
的地方。但是由于在 tabbar
上添加了自定义按钮,我无法使用以下代码隐藏自定义按钮。
ChatViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
tab.button.hidden = YES;
}
Hotspring,请参考原始标签栏对象,而不是在隐藏中心按钮时创建新标签栏对象,即
CustomTabBarViewController* tab = //reference to existing tabbar
而不是
CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
然后尝试隐藏按钮
我创建了一个自定义标签栏并在中心位置添加了一个自定义图像。以下代码的作用是 expected.It 在标签栏的中心添加我的自定义按钮。
CustomTabBarViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage* buttonImage = [UIImage imageNamed:@"icon_floating.png"];
button.frame = CGRectMake(0.0, 0.0, buttonImage.size.width, buttonImage.size.height);
[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
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:self
action:@selector(myAction)
forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void) myAction
{
self.selectedIndex = 2;
}
我遇到的问题如下。有一个 viewcontroller - ChatViewContoller
我隐藏 tabbar
的地方。但是由于在 tabbar
上添加了自定义按钮,我无法使用以下代码隐藏自定义按钮。
ChatViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
tab.button.hidden = YES;
}
Hotspring,请参考原始标签栏对象,而不是在隐藏中心按钮时创建新标签栏对象,即
CustomTabBarViewController* tab = //reference to existing tabbar
而不是
CustomTabBarViewController* tab = [[CustomTabBarViewController alloc] init];
然后尝试隐藏按钮