UIBarButtonItem с customView исчезает в iOS4.1 - PullRequest
0 голосов
/ 29 марта 2011

Я использую customView для UIBarButtonItem в моем приложении. Это прекрасно работает в 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;

1 Ответ

0 голосов
/ 06 апреля 2011

Для этого есть два возможных решения:

  1. Используйте [[UIBarButtonItem alloc] initWithCustomView::(UIView *)customView вместо назначения customView для существующего UIBarButtonItem.
  2. Добавьте строку button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); (сам не проверял)
...