删除 UITabBarItem

发布于 2024-09-29 05:25:04 字数 212 浏览 0 评论 0原文

如何从 UITabBar 中删除 UITabBarItem

我还没有尝试过任何东西,因为我没有从 Google 搜索或 UITabBarUITabBarControllerUITabBarItem 的文档中找到任何内容。

提前致谢! :)

How can I remove a UITabBarItem from a UITabBar?

I haven't tried anything, because I haven't found anything from Google searches or the documentation for UITabBar, UITabBarController, or UITabBarItem.

Thanks in advance! :)

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

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

发布评论

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

评论(3

海的爱人是光 2024-10-06 05:25:04

UITabBar 有一个 NSArray 项目集合。由于 items 属性是 NSArray 而不是 NSMutableArray,您必须从现有的没有要删除的对象的 NSArray 构造一个新的 NSArray,然后将 items 属性设置为新数组。

/* suppose we have a UITabBar *myBar, and an int index idx */
NSMutableArray *modifyMe = [[myBar items] mutableCopy];
[modifyMe removeObjectAtIndex:idx];
NSArray *newItems = [[NSArray alloc] initWithArray:modifyMe];
[myBar setItems:newItems animated:true];

UITabBar has an NSArray collection of items. Since the items property is an NSArray and not an NSMutableArray, you'd have to construct a new NSArray from the existing one devoid of the object you want to remove, then set the items property to the new array.

/* suppose we have a UITabBar *myBar, and an int index idx */
NSMutableArray *modifyMe = [[myBar items] mutableCopy];
[modifyMe removeObjectAtIndex:idx];
NSArray *newItems = [[NSArray alloc] initWithArray:modifyMe];
[myBar setItems:newItems animated:true];
櫻之舞 2024-10-06 05:25:04

Mike Caron 的建议将引发异常 如果您打算修改属于控制器的 tabBar。

在 iOS 3.0 及更高版本中,您不应该
尝试使用方法和
修改该类的属性
标签栏与
选项卡栏控制器对象。修改
标签栏以这种方式导致
抛出异常。相反,任何
对选项卡栏或其
项目应该通过标签栏出现
控制器接口。你可能还
直接修改标签栏对象
不与标签栏关联
控制器。

在这种情况下,self.tabBarItem=nil 将删除它。

Mike Caron's advice will throw an exception if you intend to modify a tabBar that belongs to a controller.

In iOS 3.0 and later, you should not
attempt to use the methods and
properties of this class to modify the
tab bar when it is associated with a
tab bar controller object. Modifying
the tab bar in this way results in the
throwing of an exception. Instead, any
modifications to the tab bar or its
items should occur through the tab bar
controller interface. You may still
directly modify a tab bar object that
is not associated with a tab bar
controller.

In this case self.tabBarItem=nil will remove it.

无所谓啦 2024-10-06 05:25:04

注意:这似乎在 iOS 11 中不起作用。在 iOS 10 中它仍然很好。

在我看来,这是一个相当糟糕的答案,部分原因是它颠覆了人机界面准则,但尽管如此,它似乎仍然可以正常工作:

UITabBar *oldbar = self.tabBarController.tabBar;
UITabBar *newbar = [[UITabBar alloc] initWithFrame:CGRectMake(0,0,oldbar.frame.size.width,oldbar.frame.size.height)];
NSMutableArray *olditems = [[oldbar items] mutableCopy];
[olditems removeObjectAtIndex:0];
NSArray *newitems = [[NSArray alloc] initWithArray:olditems];
[newbar setItems:newitems animated:false];
[oldbar addSubview:newbar];

这将它干净地放置在旧选项卡栏的顶部,并且保留了其功能。

NOTE: this appears to not work in iOS 11. It was still good in iOS 10.

This is a moderately horrible answer, in my opinion, in part because it's subverting the human interface guidelines, but all the same it seems to work cleanly:

UITabBar *oldbar = self.tabBarController.tabBar;
UITabBar *newbar = [[UITabBar alloc] initWithFrame:CGRectMake(0,0,oldbar.frame.size.width,oldbar.frame.size.height)];
NSMutableArray *olditems = [[oldbar items] mutableCopy];
[olditems removeObjectAtIndex:0];
NSArray *newitems = [[NSArray alloc] initWithArray:olditems];
[newbar setItems:newitems animated:false];
[oldbar addSubview:newbar];

That layers it cleanly on top of the old tabbar, and it maintains its functionality.

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