自定义UINavigationBar的backButton?

问题描述:

我正在制作一个iPhone应用程序,允许用户返回UINavigationBar。然而,它现在看起来很可怕。我试图用我自己的图像来定制它(减去默认的UIBarButtonItem背景)。我的图片包含了我的自定义背景,但您仍然可以看到左侧和右侧的部分按钮。自定义UINavigationBar的backButton?

UIBarButtonItem *cancelButton = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back_button_normal.png"] style:UIBarButtonItemStylePlain target:self action:@selector(cancel:)] autorelease]; 
self.navigationItem.leftBarButtonItem = cancelButton; 

有没有办法去除那个空间?有没有可能使用UIButton,以便我可以完全自定义它?我在界面生成器中做了一些事情,我将UIButton拖入UINavigationBar的右键,并且它完美地工作。无论如何,我可以通过编程来完成它吗?

谢谢!

下面是它的外观:

UINavigationBar

编辑#1:

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
[button addTarget:self action:@selector(cancel:) forControlEvents:UIControlEventTouchDown]; 
[button setTitle:@"Show View" forState:UIControlStateNormal]; 
[button setBackgroundImage:[UIImage imageNamed:@"back_button_normal.png"] forState:UIControlStateNormal]; 

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
[self.navigationItem setLeftBarButtonItem:barButtonItem]; 

下面是如何使我的UINavigationBar的背景(在我的RootViewController的地方):

@implementation UINavigationBar (UINavigationBarCustomDraw) 

- (void) drawRect:(CGRect)rect { 

[self setTintColor:[UIColor colorWithRed:0.85f green: 0.85f blue:0.85f alpha:1]]; 

if ([self.topItem.title length] > 0 && ![self.topItem.title isEqualToString:@"Back to ..."]) { 
    [[UIImage imageNamed:@"UINavigationBar_background.png"] drawInRect:rect]; 

    CGRect frame = CGRectMake(0, 0, 320, 44); 
    UILabel *label = [[[UILabel alloc] initWithFrame:frame] autorelease]; 
    [label setBackgroundColor:[UIColor clearColor]]; 
    label.font = [UIFont boldSystemFontOfSize: 20.0]; 
    label.shadowColor = [UIColor colorWithWhite:0.0 alpha:1]; 
    label.textAlignment = UITextAlignmentCenter; 
    label.textColor = [UIColor whiteColor]; 
    label.text = self.topItem.title; 
    self.topItem.titleView = label; 

} else { 
    [[UIImage imageNamed:@"login_button.png"] drawInRect:rect]; 
    self.topItem.titleView = [[[UIView alloc] init] autorelease]; 
} 
} 

@end 
+0

可能的重复:(HTTP [如何与一个UINavigationController自定义视图创建backBarButtomItem]:// stackoverflow.com/questions/526520/how-to-create-backbarbuttomitem-with-custom-view-for-a-uinavigationcontroller); [UINavigationBar后退按钮剥皮](http://stackoverflow.com/questions/2053435/uinavigationbar-back-button-skinning); [如何更改UINavigationBar中的UIBarButtonItem](http://stackoverflow.com/questions/2570247);来自**相关**边栏。 – 2011-05-15 00:13:29

如果你想添加UIButton导航b通过OBJ-C AR,你的代码应该是这样的:

UIButton *button = /* initialize your button */ 

UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 
[self.navigationItem setLeftBarButtonItem:barButtonItem]; 
+0

'no'-button'method found' on this line:'UIBarButtonItem * barButtonItem = [[UIBarButtonItem alloc] button];' – iosfreak 2011-05-14 16:25:02

+0

对不起,我吃了一段代码:)编辑。 – akashivskyy 2011-05-14 16:26:39

+0

谢谢!行动起作用,所以我知道它在那里......但由于某种原因,我看不到它......也许它是在酒吧后面呢?有任何想法吗?谢谢你的帮助。 – iosfreak 2011-05-14 16:34:04

问题是你正在使用UIButtonTypeRoundedRect代替UIButtonTypeCustom。你也可以使用:

UIImage *image = [UIImage imageNamed:@"back_button_normal.png"] 
button.frame = CGRectMake(0, 0, image.size.width, image.size.height); 

结合两个答案,并添加后退按钮功能

// Custom Navigation Bar Back Button 

// Create Button 
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
button.frame = CGRectMake(0,0,54,32); // width=54, height=32 

// Set Button Image 
NSString *backButtonURL = [[NSBundle mainBundle]pathForResource:@"back" ofType:@"png"]; 
UIImage *backButtonImage = [UIImage imageWithContentsOfFile:backButtonURL]; 
[button setBackgroundImage:backButtonImage forState:UIControlStateNormal]; 

// Important: Set Button Action to go back 
[button addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];