UIBarButton标题没有显示

时间:2015-03-17 12:26:54

标签: ios objective-c uibutton

我添加了一个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";

3 个答案:

答案 0 :(得分:1)

不需要像你一样做那么多东西,就像这样:

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

希望它有帮助.... :)

答案 1 :(得分:0)

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

创建按钮

答案 2 :(得分:0)

使用以下代码

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