UINavigationController 工具栏:设置 UIBarButtonItems 时出现问题

发布于 2024-09-18 01:35:07 字数 1797 浏览 3 评论 0原文

 -(void)viewWillAppear:(BOOL)animated {
 //setup toolbar
 [self.navigationController setToolbarHidden:NO];
 self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
 self.navigationController.toolbar.alpha = .8;
 self.navigationController.toolbar.tintColor = [UIColor colorWithRed:6.0/255.0 green:95.0/255.0 blue:163.0/255.0 alpha:1];

 //setup items
 UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self.navigationController.toolbar action:nil];
 UIImage *imageUp = [UIImage imageNamed:@"up.png"];
 UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:imageUp style:UIBarButtonItemStylePlain target:self action:@selector(toggleUp:)];
 item2.width = 5;
 UIImage *imageDown = [UIImage imageNamed:@"down.png"];
 UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
 UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithImage:imageDown style:UIBarButtonItemStylePlain target:self action:@selector(toggleDown:)];
 UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
 UIBarButtonItem *item6 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(toggleDown:)];
 UIBarButtonItem *item7 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];


 NSArray *itemsArray = [[NSArray alloc] initWithObjects:item1, item2, item3, item4, item5, item6, item7, nil ];


 [self setToolbarItems:itemsArray];


...more

} 

我不确定这里发生了什么,我已经阅读了很多有关如何处理工具栏的内容,但我似乎无法显示任何 UIBarButtonItems。

有什么想法吗?

 -(void)viewWillAppear:(BOOL)animated {
 //setup toolbar
 [self.navigationController setToolbarHidden:NO];
 self.navigationController.toolbar.barStyle = UIBarStyleBlackTranslucent;
 self.navigationController.toolbar.alpha = .8;
 self.navigationController.toolbar.tintColor = [UIColor colorWithRed:6.0/255.0 green:95.0/255.0 blue:163.0/255.0 alpha:1];

 //setup items
 UIBarButtonItem *item1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self.navigationController.toolbar action:nil];
 UIImage *imageUp = [UIImage imageNamed:@"up.png"];
 UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithImage:imageUp style:UIBarButtonItemStylePlain target:self action:@selector(toggleUp:)];
 item2.width = 5;
 UIImage *imageDown = [UIImage imageNamed:@"down.png"];
 UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
 UIBarButtonItem *item4 = [[UIBarButtonItem alloc] initWithImage:imageDown style:UIBarButtonItemStylePlain target:self action:@selector(toggleDown:)];
 UIBarButtonItem *item5 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
 UIBarButtonItem *item6 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(toggleDown:)];
 UIBarButtonItem *item7 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];


 NSArray *itemsArray = [[NSArray alloc] initWithObjects:item1, item2, item3, item4, item5, item6, item7, nil ];


 [self setToolbarItems:itemsArray];


...more

} 

I am not sure what is happening here and I have read a lot about what to do for the toolbar but I cannot seem to get any of the UIBarButtonItems to display.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-09-25 01:35:07

抱歉,上面的代码混乱了。

我已经找到了我的问题的答案。
我正在访问 UINavigationController 的子级,而不是根 UIView。
我将上面的代码分配给 viewWillAppear 到我的 UINavigationController 的根 UIViewController 并且它起作用了。因此,为了仅在我的子 UIViewController 上显示它,我必须设置

[self.navigationController setToolbarHidden:NO];

为所有我不希望工具栏可见的 UIViewController。

希望这可以帮助别人。谢谢

Sorry for the messed up code above.

I have found the answer to my question.
I was accessing a child of the UINavigationController and not the root UIView.
I assigned the code above to the viewWillAppear to the root UIViewController of my UINavigationController and it worked. So to show it only on my sub UIViewController I had to set

[self.navigationController setToolbarHidden:NO];

To all the UIViewControllers that I do not want the toolBar to be visible on.

Hope this helps someone out. Thanks

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文