【问题标题】:Custom UINavigationBar's backButton?自定义 UINavigationBar 后退按钮?
【发布时间】:2011-05-14 16:14:58
【问题描述】:

我正在制作一个允许用户返回 UINavigationBar 的 iPhone 应用程序。然而,它现在的样子是可怕的。我正在尝试用我自己的图像自定义它(减去默认的 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

【问题讨论】:

标签: objective-c cocoa-touch


【解决方案1】:

结合两个答案并添加返回按钮功能

// 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];

【讨论】:

    【解决方案2】:

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

    UIButton *button = /* initialize your button */
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
    [self.navigationItem setLeftBarButtonItem:barButtonItem];
    

    【讨论】:

    • no '-button' method found 在这条线上:UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] button];
    • 谢谢!这个动作有效,所以我知道它在那里......但由于某种原因我看不到它......也许它在酒吧后面或什么地方?有任何想法吗?感谢您的帮助。
    • 嗯。在我的回答中,我写了setRightBarButtonItem... 你用setLeftBarButtonItem 方法替换它了吗?
    • 是的,这有什么改变吗?我想用它来替换我的默认后退按钮。我在我的问题中添加了我如何制作按钮的代码。
    • 它不会改变任何东西。我想检查它是否在左边:) 让我想想,我会在几秒钟内回复你。
    【解决方案3】:

    问题是您使用的是UIButtonTypeRoundedRect 而不是UIButtonTypeCustom。您可能还想使用:

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

    【讨论】:

      猜你喜欢
      • 2013-03-18
      • 2013-09-23
      • 1970-01-01
      • 2020-11-23
      • 2012-06-16
      • 1970-01-01
      • 2012-05-01
      • 2023-04-01
      相关资源
      最近更新 更多