我已经使用不同类型的代码为我的导航栏添加自定义项而奋斗了几个小时。不幸的是它失败了,并且没有调试错误。现在我只能更改栏的标题。
代码添加到特定 ViewController 的 viewDidLoad 方法中:
UIBarButtonItem *gotoNext = [[UIBarButtonItem alloc] initWithTitle:@"Goto next" style:UIBarButtonItemStylePlain target:self action:@selector(gotoNextAction)];
self.navigationItem.rightBarButtonItem = gotoNext;
我尝试了以下代码,但不幸的是没有任何运气:
UIView* container = [[UIView alloc] init];
UIButton * button = [[UIButton alloc] init];
[button setTitle:@"Test" forState:UIControlStateNormal];
[container addSubview:button];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
self.navigationItem.rightBarButtonItem = item;