【问题标题】:UIBarButtonItem with customView disappears in iOS4.1带有 customView 的 UIBarButtonItem 在 iOS4.1 中消失了
【发布时间】:2011-03-29 00:58:50
【问题描述】:

我在我的应用程序中为 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;

【问题讨论】:

    标签: iphone objective-c uikit uibutton uibarbuttonitem


    【解决方案1】:

    对此有两种可能的解决方案:

    1. 使用 [[UIBarButtonItem alloc] initWithCustomView::(UIView *)customView 而不是将 customView 分配给现有的 UIBarButtonItem。
    2. 添加button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);这一行(我自己没有测试过)

    【讨论】:

      猜你喜欢
      • 2013-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多