如何更改 UITabBarItem 的默认标题
我想使用带有自定义标题的标准选项卡栏项目。我在直接创建 TabBarItem 之后更改标题 self.tabBarItem.title = @"Liked"。例如“收藏夹”- UITabBarSystemItemFavorites:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Liked", @"Liked");
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
self.tabBarItem.title = @"Liked";
}
return self;
}
我可以这样做吗?如果是的话我哪里出错了?
UPD:在viewDidLoad()中更改标签栏项目标题适用于iOS4,但不适用于iOS5。是否存在另一种方法?
谢谢
I want to use standard tab bar item with custom title. I change title after TabBarItem creating directly self.tabBarItem.title = @"Liked". For example "Favorites" - UITabBarSystemItemFavorites:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(@"Liked", @"Liked");
self.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemFavorites tag:0];
self.tabBarItem.title = @"Liked";
}
return self;
}
Can I do this? If yes, where I made mistake?
UPD: Changing tab bar item title in the viewDidLoad() works on iOS4 but doesn't work on iOS5. Does exist another approach?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该在 viewDidLoad 中而不是 init 中执行此操作。
You should do that in the
viewDidLoad
and not in the init.根据 Swift5.1 和 Xcode,系统选项卡栏项目的标题和图像无法更改。它们在 obj c 中定义为枚举,如下所示。这是列表:
这是有原因的。有些图标在整个 iOS 生态系统中都可以立即识别,并且对每个人来说都意味着相同的事情,因此不可能以不同的方式使用它们。
苹果通过这种方式创造了更好的用户体验。
您不能使用 Apple 图标并使用不同的标题。您可以使用自定义图标和自定义标题,或者在故事板中选择一个 SF 符号,如下所示:
As per Swift5.1 and Xcode the title and image of the system tab bar items cannot be changed. They are defined as enum in obj c as follow. This is the list:
This has a reason. Some icons are immediately recognisable across the whole iOS ecosystem and mean the same thing to everyone, therefore it is not possible to use them differently.
In this way Apple creates a better user experience.
You cannot use the Apple icon and use a different title. You could use a custom icon and custom title instead or in the Storyboards select an SF Symbol like this: