自定义UINavigationBar backButton

时间:2011-06-10 22:28:34

标签: iphone objective-c uinavigationbar

我一直在设置backgroundImages的{​​{1}},但我从未想过如何设置UINavigationBars后退按钮的backgroundImage

我怎么能这样做?

3 个答案:

答案 0 :(得分:2)

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0,0,36,30);
[button setBackgroundImage:[UIImage imageNamed:@"backgroundImage.png"] forState:UIControlStateNormal];

//[button setTitle:@"  Back" forState:UIControlStateNormal];
//[button.titleLabel setFont:[UIFont boldSystemFontOfSize:11]];

[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];

答案 1 :(得分:1)

我认为你必须为backBarButtonItem创建一个自定义按钮,然后自定义它。

如果您正在寻找教程,请转到此问题 - How do you change color/image on the default backBarButtonItem?,我认为您可以在那里找到答案。

要使条形按钮项看起来像backBarButtonItem,请参阅此答案。 Creating a left-arrow button (like UINavigationBar's "back" style) on a UIToolbar

答案 2 :(得分:0)

UIButton *backbutton = [UIButton buttonWithType:UIButtonTypeCustom];
backbutton.frame = CGRectMake(0,0,36,30);
[backbutton setBackgroundImage:[UIImage imageNamed:@"backgroundImage.png"] forState:UIControlStateNormal];

[backbutton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backbutton];
[self.navigationItem setLeftBarButtonItem:barButtonItem];