UIBarButtonItem无法正确显示

时间:2014-04-21 04:04:33

标签: ios objective-c ipad uibarbuttonitem

我有一个带有UIToolbar的iOS应用程序,上面有几个按钮。其中一个我根据日期以编程方式更改图像如下:

[_button setImage: [UIImage imageNamed: @"blah"]];

_button是一个IBOutlet。

在iPhone上它工作正常但每次我在iPad上运行应用程序时,我都会看到下图中显示的故障。知道可能导致这种情况的原因吗?

http://imagebin.org/306704

3 个答案:

答案 0 :(得分:2)

让我们试试:

UIButton *closeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[closeButton setFrame: CGRectMake(0, 0, closeButtonFontSize.width, closeButtonFontSize.height)];
[closeButton.titleLabel setFont: font];
[closeButton setTitle: closeStr forState: UIControlStateNormal];
[closeButton setTitleColor: hoverColor forState: UIControlStateHighlighted];
[closeButton addTarget:self action:@selector(closePressed) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *closeBarButton = [[UIBarButtonItem alloc] initWithCustomView:closeButton];
//set this bar button to your toolbar

答案 1 :(得分:1)

SetImage直接不用于按钮,我们为这个按钮设置图像和状态

[_button setImage:[UIImage imageNamed: @"blah"] forState:UIControlStateNormal];

带图像的UIBarButtonItem

UIBarButtonItem *leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"icon.png"] style:UIBarButtonItemStylePlain target:self action:@selector(action)];

否则你自定义你的barbuttonitem

答案 2 :(得分:1)

试试这个

UIButton *btton = [UIButton buttonWithType:UIButtonTypeCustom];
[btton setFrame:CGRectMake(0, 0, 20, 20)];
[btton addTarget:self action:@selector(actionMenu:) forControlEvents:UIControlEventTouchUpInside];
 [btton setImage:[UIImage imageNamed:@“blah.png"] forState:UIControlStateNormal];

UIBarButtonItem * barbutton = [[UIBarButtonItem alloc] initWithCustomView:btton];