自定义UINavigationBar的backButton? [英] Custom UINavigationBar's backButton?

查看:92
本文介绍了自定义UINavigationBar的backButton?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个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 ViewforState: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


解决方案

如果你想通过obj-c添加 UIButton 到导航栏,你的代码应该看起来像这样:

  UIButton * button = / *初始化你的按钮* / 

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


I am making a iPhone app that allows the user to go back in a UINavigationBar. However, the way it looks now is horrendous. I am trying to customize it with my own image (minus the default UIBarButtonItem background). My image includes my custom background, but you can still see part of the button on the left and the right.

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

Is there a way to remove that space? Is there a possibility I can use a UIButton so I can customize it completely? I did something in interface builder where i dragged a UIButton into the UINavigationBar's rightButton and it works perfectly. Is there anyway I can do it programmatically?

Thanks!

Here's how it looks:

EDIT #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];

Here's how I make my background for the UINavigationBar (places in my 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

解决方案

If you want to add UIButton to navigation bar via obj-c, your code should look like this:

UIButton *button = /* initialize your button */

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

这篇关于自定义UINavigationBar的backButton?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆