2011-05-14 106 views
6

我正在製作一個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

回答

11

如果你想添加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

2

問題是你正在使用UIButtonTypeRoundedRect代替UIButtonTypeCustom。你也可以使用:

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

結合兩個答案,並添加後退按鈕功能

// 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]; 
相關問題