我正在製作一個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的右鍵,並且它完美地工作。無論如何,我可以通過編程來完成它嗎?
謝謝!
下面是它的外觀:
編輯#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
可能的重複:(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