标题未出现在 TabBarItem 中,但图像出现

发布于 2024-12-25 01:20:51 字数 547 浏览 2 评论 0原文

我添加了一个选项卡栏控制器,并尝试向该项目添加标题和图像,但仅显示图像。标题根本就看不到。任何建议将不胜感激。

_tabbarController = [[UITabBarController alloc] init];
_showsController = [[showsController alloc] init];
_showsController.title = @"Test1";
_showsController.tabBarItem.image = [UIImage imageNamed:@"Glass.png"];
[_tabbarController setViewControllers: [NSArray arrayWithObjects: _showsController, nil]];

更新: 在stackoverflow上搜索了一段时间,终于找到了解决办法,添加如下代码: *self.view = _tabbarController.view;*

我很抱歉没有提供更多信息(这不是 rootViewController,而是作为子视图添加的),并感谢您的帮助。

I've added a tab bar controller and am trying to add a title and image to the item, but only the image shows. The title is nowhere to be seen. Any suggestions would be greatly appreciated.

_tabbarController = [[UITabBarController alloc] init];
_showsController = [[showsController alloc] init];
_showsController.title = @"Test1";
_showsController.tabBarItem.image = [UIImage imageNamed:@"Glass.png"];
[_tabbarController setViewControllers: [NSArray arrayWithObjects: _showsController, nil]];

UPDATE:
After searching stackoverflow for a while, I finally found the solution by adding the following code:
*self.view = _tabbarController.view;*

I apologize for not giving more information (that this was not the rootViewController but added as a subview instead), and thank you for your help.

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

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

发布评论

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

评论(2

生死何惧 2025-01-01 01:20:51

您需要设置 UITabBarItem 标题:

 _showsController.tabBarItem.title = @"Test1";

或者一次性设置它们:

_showsController.title = _showsController.tabBarItem.title = @"Test1";

You need to set the UITabBarItem title:

 _showsController.tabBarItem.title = @"Test1";

Or set them both in one go:

_showsController.title = _showsController.tabBarItem.title = @"Test1";
寂寞花火° 2025-01-01 01:20:51

您还需要在 UITabBarItem 对象上设置标题。

_showsController.tabBarItem.title = _showsController.title;

就我个人而言,我更喜欢让 UIViewController 子类在可能的情况下处理自己的标题。这使您的代码更加独立,并且并不总是强制 UIViewController 的创建者正确初始化它。

例如,在您的 UIViewController 子类中:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Test1"
                                                           image:[UIImage imageNamed:@"Glass.png"]
                                                             tag:1];
        [self setTabBarItem:item];
        [item release];
    }

    return self;
}

You also need to set the title on the UITabBarItem object.

_showsController.tabBarItem.title = _showsController.title;

Personally, I prefer to let the UIViewController subclass handle its own titles when possible. This makes your code more self-contained and doesn't always force the UIViewController's creator to initialize it properly.

For example, in your UIViewController subclass:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
    {
        UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:@"Test1"
                                                           image:[UIImage imageNamed:@"Glass.png"]
                                                             tag:1];
        [self setTabBarItem:item];
        [item release];
    }

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