在工具栏中添加自定义标签不起作用

2023-12-09

我正在尝试在 UINavigationController 的工具栏中添加自定义标签。我遵循了这个问题的最佳答案question但它似乎对我不起作用,我不知道为什么。自定义文本不会出现,但按钮会出现。当我按下它时它会突出显示,但没有文字。

这是我的代码:

- (void) editToolbar{
    NSArray *toolbarItemsCopy = [self.toolbarItems copy];

    //set up the label
    self.notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width/0.25, 21.0f)];
    self.notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:20];
    self.notificationsLabel.backgroundColor = [UIColor clearColor];
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
    self.notificationsLabel.textColor = [UIColor blueColor];
    self.notificationsLabel.text = @"Checking server";
    self.notificationsLabel.textAlignment = UITextAlignmentCenter;

    //init a button with custom view using the label
    UIBarButtonItem *notif = [[UIBarButtonItem alloc] initWithCustomView:self.notificationsLabel];

    //add to the toolbarItems
    NSMutableArray *toolbarItemsMutableArray = [[NSMutableArray alloc]init];

    NSLog(@"toolbar %i", self.toolbarItems.count);

    //have to add the custom bar button item in a certain place in the toolbar, it should be the 3rd item
    for (int i = 0; i < toolbarItemsCopy.count; i++) {
        if (i == 2){                
            [toolbarItemsMutableArray addObject:notif];
        }
        NSLog(@"toolbar item %i %@", i,[toolbarItemsCopy objectAtIndex:i]);
        [toolbarItemsMutableArray addObject:[toolbarItemsCopy objectAtIndex:i]];
    }
    if (toolbarItemsCopy.count == 4){
    }else{
        //remove the previous custom label
        [toolbarItemsMutableArray removeObjectAtIndex:3];
    }
    self.toolbarItems = toolbarItemsMutableArray;
    //[self.navigationController.toolbar setItems: toolbarItemsMutableArray animated:YES];

    NSLog(@"toolbar %i", self.toolbarItems.count);
}https://stackoverflow.com/questions/ask

这是 NSLog 打印的内容:

Catalogue[361:10403] toolbar 4
Catalogue[361:10403] toolbar item 0 <UIBarButtonItem: 0x8a24650>
Catalogue[361:10403] toolbar item 1 <UIBarButtonItem: 0x8a36cd0>
Catalogue[361:10403] toolbar item 2 <UIBarButtonItem: 0x8a36d30>
Catalogue[361:10403] toolbar item 3 <UIBarButtonItem: 0x8a382f0>
Catalogue[361:10403] toolbar 5

这就是我解决它的方法:

我必须在函数内分配并初始化 UILabel,而不是使用 ivar。

UILabel *notificationsLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f, self.view.frame.size.width, 21.0f)];
    notificationsLabel.font = [UIFont fontWithName:@"Helvetica" size:16];
    notificationsLabel.backgroundColor = [UIColor clearColor];
    //self.notificationsLabel.textColor = [UIColor colorWithRed:157.0/255.0 green:157.0/255.0 blue:157.0/255.0 alpha:1.0];
    notificationsLabel.textColor = [UIColor whiteColor];
    notificationsLabel.text = @"Checking server";
    notificationsLabel.textAlignment = UITextAlignmentCenter;

我确信这段代码会对您有所帮助。在牢记此代码的同时更改您的代码。我已经研究过这个并且效果很好,

UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)];
[self.view addSubview:toolBar];

UILabel *titleLbl = [[UILabel alloc] initWithFrame:CGRectMake(0.0 , 11.0f,     self.view.frame.size.width, 21.0f)];
[titleLbl setFont:[UIFont fontWithName:@"Helvetica-Bold" size:18]];
[titleLbl setBackgroundColor:[UIColor clearColor]];
[titleLbl setTextColor=[UIColor blueColor];
[titleLbl setText:@"Title"];
[titleLbl setTextAlignment:UITextAlignmentCenter];

UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:titleLbl];
NSArray *toolbarItemArr = [NSArray arrayWithObjects:button, nil];
[toolBar setItems:toolbarItemArr animated:YES];

为了更好地理解,您可以点击以下链接:UIToolBar类参考 and UIBarButtonItem 类参考

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在工具栏中添加自定义标签不起作用 的相关文章

随机推荐

  • 在 VB.Net 中等待变量改变状态的更好方法

    我有一个循环遍历多个值 迭代每个值时 都会将页面加载到 Web 浏览器控件中 将值作为参数传递 并且当加载和读取页面时 循环应转到列表中的下一个值并继续 直到处理完所有值 我需要一种方法来在网站异步加载时暂停该过程 然后在页面加载 读取过程
  • Google Sheet:onChange 事件触发时如何获取实际更改?

    the doc不是很有帮助 不像onEdit onChange事件没有包含所做更改的属性 我怎样才能得到它 function onChange e console log e 该事件对象中没有任何有用的信息 我无法使用 onEdit 的原因
  • 如何在d3js中制作分组堆积条形图?

    我有以下 d3 图表 它已分组 并且每个分组都包含一个堆积条形图 但不知何故 我觉得这不是一个正确的实施方式 而且有点复杂 如果只有堆积条形图 我会使用d3 stack 有人可以让我知道有没有更好的方法来做到这一点 片段如下 var dat
  • git 变基基础知识

    我已经开始使用git rebase最近 我不能 100 确定我做得对 为了问题起见 起源有两个分支 master and next 它是从master 自从两者上次同步以来 master有 2 次提交并且next 6 git log one
  • 在vhdl中生成随机整数

    我需要在 vhdl 中生成 0 1023 之间的随机整数 但是我在互联网上找不到这方面的好资源 请问有人帮我吗 下面是生成范围 0 1023 内均匀 均匀 分布的整数的示例 请注意 floor必须在与最大值 1 相乘之后使用运算 在本例中为
  • 在asp.net中隐藏gridView行

    我正在创建一个gridView允许通过添加插入所需的控件来添加新行FooterTemplate 但是当ObjectDataSource没有记录 我添加一个虚拟行作为FooterTemplate仅当有数据时才显示 我怎样才能隐藏这个虚拟行 我
  • XML 数据提取,其中并非所有父节点都包含子节点

    我有一个 xml 数据文件 其中用户已开设帐户 但在某些情况下该帐户已被终止 数据没有列出帐户未终止时的值 这使得提取信息非常困难 以下是可重现的示例 其中只有用户 1 和 3 的帐户被终止 library XML my xml lt xm
  • 如何在 Activity 启动时滚动到 ScrollView 的底部

    我正在 ScrollView 中显示一些数据 在活动启动时 方法 onCreate 我用数据填充 ScrollView 并希望滚动到底部 我尝试使用getScrollView fullScroll ScrollView FOCUS DOWN
  • apache2 中的初始化模块

    我曾经在apache 1 3中编写apache模块 但这些天我愿意传递到apache2 出于性能目的 我现在正在编写的模块有自己的二进制数据 而不是数据库 我需要将这些数据加载到共享内存中 这样每个孩子都可以访问它 而无需制作自己的副本 并
  • 在 R tm 包中,从 Document-Term-Matrix 构建语料库

    使用 tm 包从语料库构建文档术语矩阵非常简单 我想从文档术语矩阵构建一个语料库 令 M 为文档集中的文档数 令 V 为该文档集词汇表中的术语数量 那么文档术语矩阵就是 M V 矩阵 我还有一个长度为 V 的词汇向量 词汇向量中是由文档术语
  • PHP 二进制到十六进制(带前导零)

    我有以下代码 效果很好 我得到了 c00 的值 但是 当我尝试转换 000000010000 时 我得到的值是 10 我真正想要的是所有前导零 所以我可以获得 010 作为最终结果 我该怎么做呢 编辑 我应该指出 二进制数的长度可以变化 所
  • 平稳的飞行运动,如飞翔的小鸟或喷气背包的重力和加速度欢乐之旅

    我正在开发一个简单的游戏 当您点击 单击屏幕时 角色就会飞翔 继续点击角色就会飞起来 有些类似于飞翔的小鸟和喷气背包 然而 就像喷气背包一样 运动一点也不顺畅 这是我的代码示例 变量初始化 maxSpeedLimit spriteHeigh
  • 在java中查询JSONObject

    我想知道是否存在一个能够查询 JSONObject 的 java 库 更深入地我正在寻找类似的东西 String json data data2 value hello Somehow we managed to convert json
  • 多个数据表和引导选项卡的响应问题

    我有 2 个引导井 每个井包含一对标签 每个选项卡包含一个数据表 总共有 4 个 当我加载页面时 前两个数据表按预期完美工作 当我切换选项卡时 呈现的表始终包含 1 列 无论列大小如何 其余部分是子行的一部分 我已经尝试了几乎所有方法来让表
  • 在关联数组内搜索和替换

    我需要在关联数组内搜索和替换 ex user user1 I ve updated this myarray array user1 gt search1 user2 gt search2 user3 gt search1 我想更换sear
  • 如何设置jqGrid中搜索框的默认列?

    我已指定可通过以下方式搜索哪些列colModel 但我找不到打开搜索框时指定默认列的方法 任何帮助表示赞赏 有选项columns这没有记录在搜索选项列表 我最近写的答案 and the demo它演示了如何实现您的要求 UPDATED 如果
  • Silverlight/WPF 设置十六进制颜色的椭圆

    我试图在后面的代码中设置椭圆对象的颜色 到目前为止 我正在使用 SolidColorBrush 方法来完成此操作 有没有办法像 CSS 一样以十六进制插入颜色值 这是我正在使用的代码 ellipse Fill new SolidColorB
  • @Query 和存储库中的错误

    可能有错误 Query由于我的实体有关系 存储库包含错误 创建名称为 clickRepository 的 bean 时出错 调用 init 方法失败 嵌套异常是java lang IllegalArgumentException 方法公共摘
  • DB单元;表/列名称区分大小写的混淆

    当我启动我的应用程序时 我收到此错误 Caused by org dbunit dataset NoSuchColumnException CLIENT ID Non uppercase input column ID in ColumnN
  • 在工具栏中添加自定义标签不起作用

    我正在尝试在 UINavigationController 的工具栏中添加自定义标签 我遵循了这个问题的最佳答案question但它似乎对我不起作用 我不知道为什么 自定义文本不会出现 但按钮会出现 当我按下它时它会突出显示 但没有文字 这