我想将动态文本放在一个条形按钮项目中,类似于纽约时代对其评论数量的影响,如下图所示。
关于如何实现此功能的任何想法?
答案 0 :(得分:1)
我认为关键是UIBarButtonItem
的{{3}}方法。
这是伪代码,根据您的需要进行修改:
UICustomViewController *customVC = [[UICustomViewController alloc] initWithNibName:@"UICustomViewController" bundle:nil];
[customVC loadView]; //load the views, so your views won't be nil
[customVC updateText];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:customVC.view];
[[self navigationItem] setRightBarButtonItem:barButtonItem];
答案 1 :(得分:0)
创建UIButton并将其分配给导航栏右栏按钮Item。每当有更新时,都会更改按钮文本。我使用导航控制器和导航栏顶部的自定义按钮创建了一个示例项目test该功能
//Create a button and assign it as rightbarbutton Item.
self.customButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 70, 40)];
self.customButton.titleLabel.textColor=[UIColor whiteColor];
[self.customButton setTitle:@"0" forState:UIControlStateNormal];
self.customButton.backgroundColor=[UIColor blueColor];
self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:self.customButton];
//Display some random number on navigation bar button
NSString *randomNumber=[NSString stringWithFormat:@"%u",arc4random_uniform(1000)];
[self.customButton setTitle:randomNumber forState:UIControlStateNormal];
您可以从here
下载示例项目