自定义导航栏上的导航项

时间:2013-05-31 02:32:32

标签: ios objective-c uinavigationcontroller

如图所示,我已成功通过代码自定义UINavigationController的navigationBar的背景:

UINavigationController *nav = ......
[[nav navigationBar] setBackgroundImage:[UIImage imageNamed:@"bg_daohang"] forBarMetrics:UIBarMetricsDefault];

enter image description here

现在,我想在navigationBar上自定义项目(在红色圆圈中)。是否可以这样做以及如何做到这一点?

1 个答案:

答案 0 :(得分:1)

UIButton *button =[UIButton buttonWithType:UIButtonTypeCustom];
button.frame=CGRectMake(0.0, 0, 60.0, 30.0);
[button setBackgroundImage:[UIImage imageNamed:@"logout.png"] forState:UIControlStateNormal];
[button setTitle:@"Logout" forState:UIControlStateNormal];
button.titleLabel.font = [UIFont boldSystemFontOfSize:13];

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

UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem= item;
[item release];