将UIImage设置为UIBarButton项 [英] Setting a UIImage to a UIBarButton item

查看:105
本文介绍了将UIImage设置为UIBarButton项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法向此UIBarButtonItem添加图像而不会在触摸时崩溃:

I can't seem to add an image to this UIBarButtonItem without it crashing when touched:

 UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
                                                                       style:UIBarButtonItemStylePlain target:nil action:nil];
        self.navigationItem.backBarButtonItem = backButton;
        [backButton release];

此代码添加了一个图像,但由于指向UIBarButtonItem的指针类型不兼容而触摸时会崩溃来自UIButton:

this code adds an image, but it crashes when touched because of an incompatible pointer types assigning to UIBarButtonItem * from UIButton:

    //Setup Custom Button

    UIButton *button = [[UIButton alloc] init];

    button.frame = CGRectMake(0,0,44,44);

//    [button addTarget:self action:@selector(showInfo) forControlEvents:UIControlEventTouchUpInside];

    self.navigationItem.backBarButtonItem = button;

    [button setBackgroundImage:imgIcon forState:UIControlStateNormal];

    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];

    [button release];

感谢您的帮助

推荐答案

致电

self.navigationItem.leftBarButtonItem = [CustomBarButton createNavBackBarButtonItemWithTitle:NSLocalizedString(@"Back", @"") target:self action:@selector(actionBack:)];

CustomBarButton具有createNavBackBarButtonItemWithTitle类函数

CustomBarButton has createNavBackBarButtonItemWithTitle class function

+(UIBarButtonItem *)createNavBackBarButtonItemWithTitle:(NSString *)t target:(id)tgt action:(SEL)a
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Since the buttons can be any width we use a thin image with a stretchable center point
UIImage *buttonImage = [[UIImage imageNamed:@"back_button_up.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];
UIImage *buttonPressedImage = [[UIImage imageNamed:@"back_button_over.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0];


[[button titleLabel] setFont:[UIFont boldSystemFontOfSize:12.0]];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
//[[button titleLabel] setShadowOffset:CGSizeMake(0.0, 1.0)];

CGRect buttonFrame = [button frame];
buttonFrame.size.width = [t sizeWithFont:[UIFont boldSystemFontOfSize:12.0]].width + 24.0;
buttonFrame.size.height = buttonImage.size.height;
[button setFrame:buttonFrame];

[button setBackgroundImage:buttonImage forState:UIControlStateNormal];
[button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted];

[button setTitle:[NSString stringWithFormat:@"  %@",t] forState:UIControlStateNormal];

[button addTarget:tgt action:a forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

return [buttonItem autorelease];
}

这篇关于将UIImage设置为UIBarButton项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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