UIBarButton 标题不显示

UIBarButton title doesn't get displayed

我添加了一个 UIBarButton。但是,我无法显示标题。

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];

[button addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithCustomView:button];

self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;

我尝试了以下方法:

button.title =@"Title";

customItem.title=@"title";

使用 UIButton setTitle:forState: 而不是访问 属性。此外,您应该使用方法 [UIButton buttonWithType:<#(UIButtonType)#>] 而不是 UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 30, 30)];

创建按钮

不需要像你一样做那么多事情,只要喜欢:

UIBarButtonItem *customItem=[[UIBarButtonItem alloc]initWithTitle:@"Title" style:UIBarButtonItemStylePlain target:self action:@selector(doneButtonClicked:)];
self.navigationItem.rightBarButtonItem=customItem;

希望对您有所帮助....:)

使用下面的代码

UIBarButtonItem *customItem = [[UIBarButtonItem alloc]
                            initWithTitle:@"Title"
                            style:UIBarButtonItemStyleBordered
                            target:self
                            action:@selector(doneButtonClicked:)];
self.navigationController.visibleViewController.navigationItem.rightBarButtonItem = customItem;