我在我的应用中使用了一个用于UIBarButtonItem的customView。这在iOS4.2中运行良好(并且没有关于iOS4.0的任何错误报告),但在iOS4.1中应用customView使整个按钮消失。有谁知道为什么会发生这种情况/我做错了什么?这是我的代码:
//array with button particulars:
// - the UIBarButtonItem to apply
// - image for normal state
// - image for highlighted state
// - button title
// - method to handle the click/tap
NSArray *buttonArr = [NSArray arrayWithObjects:aUIBarButtonItem, @"btn_img.png", @"btn_img_over.png", @"btn_title", @"clickHandler:", nil];
//configure the UIButton to use as customView
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.titleLabel.font = [UIFont boldSystemFontOfSize:10.0];
button.titleLabel.textColor = [UIColor whiteColor];
button.titleLabel.textAlignment = UITextAlignmentCenter;
[button setTitle:[buttonArr objectAtIndex:3] forState:UIControlStateNormal];
// make the buttons content appear in the top-left
[button setContentHorizontalAlignment:UIControlContentHorizontalAlignmentLeft];
[button setContentVerticalAlignment:UIControlContentVerticalAlignmentTop];
CGSize textSize = [[button.titleLabel text] sizeWithFont:[button.titleLabel font]];
CGFloat labelWidth = textSize.width;
buttonImage = [UIImage imageNamed:[buttonArr objectAtIndex:1]];
[button setImage:buttonImage forState:UIControlStateNormal];
buttonImage = [UIImage imageNamed:[buttonArr objectAtIndex:2]];
[button setImage: buttonImage forState:UIControlStateHighlighted];
[button setImage: buttonImage forState:UIControlStateDisabled];
[button addTarget:self action:NSSelectorFromString([buttonArr objectAtIndex:4]) forControlEvents:UIControlEventTouchUpInside];
// move text 10 pixels down and right
[button setTitleEdgeInsets:UIEdgeInsetsMake(50.0, -40.0, 0.0, 0.0)];
[button setImageEdgeInsets:UIEdgeInsetsMake(5.0, (labelWidth - buttonImage.size.width)/2 , 0.0 ,0.0)];
button.frame = CGRectMake(0.0, 0.0, MAX(labelWidth, buttonImage.size.width), buttonImage.size.height + 20);
//apply customView to UIBarButtonItem
barButton = (UIBarButtonItem *)[buttonArr objectAtIndex:0];
barButton.customView = button;
答案 0 :(得分:0)
有两种可能的解决方案:
[[UIBarButtonItem alloc] initWithCustomView::(UIView *)customView
而不是将customView分配给现有的UIBarButtonItem。button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
行(尚未自行测试)