我正在制作一款iPhone应用程序,允许用户返回UINavigationBar。然而,它现在的样子是可怕的。我试图用我自己的图像(减去默认的UIBarButtonItem背景)来自定义它。我的图片包含我的自定义背景,但您仍然可以看到左侧和右侧的部分按钮。
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的rightButton,它完美地运行。无论如何我可以通过编程方式进行吗?
谢谢!
以下是它的外观:
编辑#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 :(得分: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];
答案 1 :(得分:11)
如果您想通过obj-c将UIButton
添加到导航栏,您的代码应如下所示:
UIButton *button = /* initialize your button */
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
答案 2 :(得分:2)
问题是您使用UIButtonTypeRoundedRect
代替UIButtonTypeCustom
。您可能还想使用:
UIImage *image = [UIImage imageNamed:@"back_button_normal.png"]
button.frame = CGRectMake(0, 0, image.size.width, image.size.height);