按钮不会隐藏在自定义 TabBar 中
使用 这个教程我设置了一个自定义TabBar。不幸的是,本教程不会描述如何在您不想显示的视图中隐藏自定义 TabBar。
在我的 customTabBar.h
中,我定义了
- (void) hideAlsoCustomTabBar;
- (void) showCustomTabBarAgain;
哪些实现为
- (void) hideAlsoCustomTabBar {
btn1.hidden = YES;
btn2.hidden = YES;
btn3.hidden = YES;
btn4.hidden = YES;
}
- (void) showCustomTabBarAgain {
btn1.hidden = NO;
btn2.hidden = NO;
btn3.hidden = NO;
btn4.hidden = NO;
}
Calling those inside CustomTabBar.m
's viewDidAppear
工作正常并且完全符合我的期望去做。 隐藏 TabBar
[customTabs hideAlsoCustomTabBar];
如果我尝试从 ViewController 调用这些方法,我需要像这样在 initWithNibName
或 viewDidLoad
或 viewWillAppear
内 ,则什么也不会发生。我检查了 NSLog,该方法被调用,但是当我读出任何按钮 .hidden
属性的 BOOL 时,它返回 0,而它应该是 1 (对于 hidden==YES< /代码>)。 我不知道我的设置有什么问题。这些按钮的属性是否可能默认为私有属性,因为
CustomTabBar
继承自 UITabBarController
并且我无法设置它们?还是我在通话中犯了错误?
谢谢! 编辑 中所述
import "CustomTabBar.h"
@implementation CustomTabBar
@synthesize btn1,btn2,btn3,btn4;
- (void) viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideExistingTabBar];
[self addCustomElements];
}
- (void) hideExistingTabBar {
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
view.hidden = YES;
break;
}
}
}
- (void) addCustomElements {
//Initialise the two images for btnEinleitung, not selected and selected
UIImage *btnImage = [UIImage imageNamed:@"btnEinl.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"btnEinl_s.png"];
self.btnEinleitung = [UIButton buttonWithType:UIButtonTypeCustom];
btnEinleitung.frame = CGRectMake(0, 430, 86, 50);
[btnEinleitung setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnEinleitung setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnEinleitung setTag:0];
[btnEinleitung setSelected:true];
//set other buttons
btnImage = [UIImage imageNamed:@"btnUbg.png"];
btnImageSelected = [UIImage imageNamed:@"btnUbg_s.png"];
self.btnUebungen = [UIButton buttonWithType:UIButtonTypeCustom];
btnUebungen.frame = CGRectMake(86, 430, 80, 50);
[btnUebungen setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnUebungen setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnUebungen setTag:1];
/* do the same for btn3 and btn4*/
//add custom buttons to view
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];
//setup event handlers so the buttonClicked method will respond to the touch up inside event
[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
//set tab to active according to the passed tag number
- (void) selectTab:(int)tabID {
switch (tabID) {
case 0:
[btnEinleitung setSelected:TRUE];
[btnUebungen setSelected:FALSE];
[btnTipps setSelected:FALSE];
[btnBrauchtum setSelected:FALSE];
btnEinleitung.userInteractionEnabled = NO;
btnUebungen.userInteractionEnabled = YES;
btnTipps.userInteractionEnabled = YES;
btnBrauchtum.userInteractionEnabled = YES;
break;
case 1:
[btnEinleitung setSelected:FALSE];
[btnUebungen setSelected:TRUE];
[btnTipps setSelected:FALSE];
[btnBrauchtum setSelected:FALSE];
btnEinleitung.userInteractionEnabled = YES;
btnUebungen.userInteractionEnabled = NO;
btnTipps.userInteractionEnabled = YES;
btnBrauchtum.userInteractionEnabled = YES;
break;
// and so on for 2 and 3
}
self.selectedIndex = tabID;
}
//get the tag of the sender/pressed button, call the function selectTab
- (void) buttonClicked:(id)sender {
int tagNum = [sender tag];
[self selectTab:tagNum];
}
TabBar 实现如教程编辑 如下所述,我在一个选项卡栏中有 4 个选项卡,这些选项卡是用 IB 生成的,添加了导航控制器及其 ViewController,为这些选项卡制作了 Outlet 并将它们连接到 IB 中。 单击第二个选项卡(例如 sndMenuVC)将打开一个包含 4 个按钮的视图。单击这些按钮之一会导致另一个视图(例如detailVC),我不希望在其中显示TabBar。 DetailVC 有它自己的笔尖。
打开detailVC发生在像这样声明的按钮操作中
- (IBAction) openFourth:(id)sender{
detailVC *detailView = [[detailVC alloc] initWithNibName:@"detailVC" bundle:nil andSender:kFourthButtonSender];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
,因为这是一个自定义的initWithNibName,这就是我实现它的方式,
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andSender: (NSString *)calledByButton{
self.callingButton = calledByButton;
[super initWithNibName:@"detailVC" bundle:nil];
return self;
}
所以我基本上只是根据传输的发送者设置一个全局变量“callingButton”,然后调用“正常”initWithNibName。
这一切都很好。
如果我尝试调用 hideAlsoCustomTabBar
,并且 NSLog 记录 btn1.hidden 的值,则在从DetailVC 调用时返回 0,但在从 CustomTabBar 内调用时返回 1 并且实际上隐藏了按钮。
如果需要,我将 customTabs 作为 IBOutlet,但不知道它是否正确连接到 CustomTabBar 类型的 TabBarController。
希望这有助于理解我:) 如果您需要任何其他信息,请告诉我。 谢谢!
using this tutorial I set up a custom TabBar. Unfortunately the tutorial won't describe how to hide the custom TabBar in views you don't wanna display it.
In my customTabBar.h
I defined
- (void) hideAlsoCustomTabBar;
- (void) showCustomTabBarAgain;
which are implemented as
- (void) hideAlsoCustomTabBar {
btn1.hidden = YES;
btn2.hidden = YES;
btn3.hidden = YES;
btn4.hidden = YES;
}
- (void) showCustomTabBarAgain {
btn1.hidden = NO;
btn2.hidden = NO;
btn3.hidden = NO;
btn4.hidden = NO;
}
Calling those inside CustomTabBar.m
's viewDidAppear
works fine and does exactly what I expect them to do. If I try to call those methods from the ViewController where I need to hide the TabBar like this
[customTabs hideAlsoCustomTabBar];
inside the initWithNibName
OR viewDidLoad
OR viewWillAppear
, nothing will happen. I checked with NSLog, the method gets called, but when I read out the BOOL for any buttons .hidden
attribute, it returns 0, when it should be 1 (for hidden==YES
).
I don't know what's wrong with my setup. Is it possible these button's attributes are private by default as CustomTabBar
inherits from UITabBarController
and I can't set them? Or did I make a mistake in the call?
Thanks!
EDIT
The TabBar implementation as it is described in the tutorial
import "CustomTabBar.h"
@implementation CustomTabBar
@synthesize btn1,btn2,btn3,btn4;
- (void) viewDidAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self hideExistingTabBar];
[self addCustomElements];
}
- (void) hideExistingTabBar {
for (UIView *view in self.view.subviews) {
if ([view isKindOfClass:[UITabBar class]]) {
view.hidden = YES;
break;
}
}
}
- (void) addCustomElements {
//Initialise the two images for btnEinleitung, not selected and selected
UIImage *btnImage = [UIImage imageNamed:@"btnEinl.png"];
UIImage *btnImageSelected = [UIImage imageNamed:@"btnEinl_s.png"];
self.btnEinleitung = [UIButton buttonWithType:UIButtonTypeCustom];
btnEinleitung.frame = CGRectMake(0, 430, 86, 50);
[btnEinleitung setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnEinleitung setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnEinleitung setTag:0];
[btnEinleitung setSelected:true];
//set other buttons
btnImage = [UIImage imageNamed:@"btnUbg.png"];
btnImageSelected = [UIImage imageNamed:@"btnUbg_s.png"];
self.btnUebungen = [UIButton buttonWithType:UIButtonTypeCustom];
btnUebungen.frame = CGRectMake(86, 430, 80, 50);
[btnUebungen setBackgroundImage:btnImage forState:UIControlStateNormal];
[btnUebungen setBackgroundImage:btnImageSelected forState:UIControlStateSelected];
[btnUebungen setTag:1];
/* do the same for btn3 and btn4*/
//add custom buttons to view
[self.view addSubview:btn1];
[self.view addSubview:btn2];
[self.view addSubview:btn3];
[self.view addSubview:btn4];
//setup event handlers so the buttonClicked method will respond to the touch up inside event
[btn1 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn2 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn3 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[btn4 addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
}
//set tab to active according to the passed tag number
- (void) selectTab:(int)tabID {
switch (tabID) {
case 0:
[btnEinleitung setSelected:TRUE];
[btnUebungen setSelected:FALSE];
[btnTipps setSelected:FALSE];
[btnBrauchtum setSelected:FALSE];
btnEinleitung.userInteractionEnabled = NO;
btnUebungen.userInteractionEnabled = YES;
btnTipps.userInteractionEnabled = YES;
btnBrauchtum.userInteractionEnabled = YES;
break;
case 1:
[btnEinleitung setSelected:FALSE];
[btnUebungen setSelected:TRUE];
[btnTipps setSelected:FALSE];
[btnBrauchtum setSelected:FALSE];
btnEinleitung.userInteractionEnabled = YES;
btnUebungen.userInteractionEnabled = NO;
btnTipps.userInteractionEnabled = YES;
btnBrauchtum.userInteractionEnabled = YES;
break;
// and so on for 2 and 3
}
self.selectedIndex = tabID;
}
//get the tag of the sender/pressed button, call the function selectTab
- (void) buttonClicked:(id)sender {
int tagNum = [sender tag];
[self selectTab:tagNum];
}
EDIT
As described below, I have 4 Tabs in a Tabbar which was generated with IB, added Navigation Controller with their ViewControllers, made Outlets for those and connected them in IB.
Clicking on the second Tab (e.g. sndMenuVC) opens a view with 4 buttons. Clicking one of these buttons leads to another view (e.g. detailVC) in which I don't want the TabBar to be displayed. detailVC has it's own nib.
Opening detailVC happens with the button's action declared like this
- (IBAction) openFourth:(id)sender{
detailVC *detailView = [[detailVC alloc] initWithNibName:@"detailVC" bundle:nil andSender:kFourthButtonSender];
[self.navigationController pushViewController:detailView animated:YES];
[detailView release];
}
As this is a custom initWithNibName, this is how I implemented it
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andSender: (NSString *)calledByButton{
self.callingButton = calledByButton;
[super initWithNibName:@"detailVC" bundle:nil];
return self;
}
SO I basically just set a global variable "callingButton" according to the transmitted sender and call the "normal" initWithNibName afterwards.
This all works fine.
If I try to call hideAlsoCustomTabBar
, and NSLog the value of btn1.hidden it returns 0 when called from detailVC, but returns 1 when called from within CustomTabBar and actually hides the buttons.
I have customTabs as IBOutlet if needed, but don't know if this is connected correctly to the TabBarController of type CustomTabBar.
Hope this helps to understand me :) If you need any other information, just let me know.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我已经编写了 RXCustomTabBar 教程的后续内容,应该可以回答您的问题。我认为在这里完全复制它没有任何意义。
隐藏您的新自定义 UITabBar(RXCustomTabBar 后续)
如果您有任何疑问,请告诉我,
奥利
I have written a follow up to my RXCustomTabBar tutorial which should answer your questions. I don't see any point reproducing it in full here.
Hiding your New Custom UITabBar (RXCustomTabBar follow up)
Let me know if you have any questions,
Oli
如果你想隐藏 tabBar 你可以简单地在你的视图控制器中调用
if you want to hide the tabBar you can simply, in your view controller, call
在 RXCustomTabBar.m 文件中,从 viewDidAppear 调用 addCustomElements 函数。
希望这有帮助。
更新
假设您想隐藏第二个视图控制器上的按钮。所以我在情况 1 索引中调用了 [self hideButtons]。
这将隐藏所有标签栏按钮。反之亦然,你必须设置差异条件来显示这些选项卡按钮。
这有道理吗?
In RXCustomTabBar.m file addCustomElements function is called from viewDidAppear.
Hope this helps.
Update
Let say you want to hide buttons on 2nd View Controller. So I have called [self hideButtons] in case 1 index.
This will Hide all the tabbar buttons. And again Viceversa you have to put diff condition to show those tab buttons.
Does this make sense ?
由于调用此方法时自定义选项卡已经存在。您应该在此处将其分配给
detailView
的customTabs
属性。原答案
由于对
UITabBarController
子类的引用为nil
,因此您必须正确设置它。如果您已使用 IB 设置视图控制器,请转到 NIB 文件。右键单击视图控制器,选择customTabs
出口并将其连接到保存视图控制器的customTabBar
对象。如果您以编程方式创建了视图控制器,那么在将视图控制器添加到选项卡栏之后、释放视图控制器之前的某个位置,执行以下操作:
Since the custom tabs exists already at the point this method is called. You should assign it to
detailView
'scustomTabs
property here.Orignal Answer
As the reference to the subclass of the
UITabBarController
isnil
, you will have to set it properly. If you have used IB to set the view controllers, go to the NIB file. Right click on the view controller, select thecustomTabs
outlet and connect it to thecustomTabBar
object holding the view controllers.If you've created the view controllers programmatically then somewhere after adding the view controllers to the tab bar and before releasing the view controllers, do this,