gpt4 book ai didi

iphone - 将 UIImage 设置为 UIBarButton 项目

转载 作者:行者123 更新时间:2023-11-28 22:56:59 26 4
gpt4 key购买 nike

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

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

此代码添加了一个图像,但由于从 UIButton 分配给 UIBarButtonItem * 的不兼容指针类型,它在触摸时崩溃:

    //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类函数

+(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];
}

关于iphone - 将 UIImage 设置为 UIBarButton 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10585678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com