【发布时间】:2015-09-07 23:22:57
【问题描述】:
我正在为 UINavigation 栏创建自定义右栏按钮项。这是我正在使用的代码,
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 40)];
UIButton *backPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backPageButton setFrame:CGRectMake(0, 0, 50, 30)];
[ backPageButton setImage:[UIImage imageNamed:@"left_arrow.png"] forState:UIControlStateNormal];
backPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[backPageButton addTarget:self action:@selector(backButtonAction) forControlEvents:UIControlEventTouchUpInside];
[backPageButton setUserInteractionEnabled:YES];
[container addSubview:backPageButton];
UIButton *forwardPageButton = [UIButton buttonWithType:UIButtonTypeCustom];
[forwardPageButton setFrame:CGRectMake(30, 0, 50, 30)];
[ forwardPageButton setImage:[UIImage imageNamed:@"right_arrow.png"] forState:UIControlStateNormal];
[forwardPageButton addTarget:self action:@selector(forwardButtonAction) forControlEvents:UIControlEventTouchUpInside];
forwardPageButton.imageView.contentMode = UIViewContentModeScaleAspectFill;
[forwardPageButton setUserInteractionEnabled:YES];
[container addSubview:forwardPageButton];
UIBarButtonItem* item = [[UIBarButtonItem alloc] initWithCustomView:container];
// set the nav bar's right button item
self.navigationItem.rightBarButtonItem = item;
由于某种原因,按钮图像不可见。我为每个按钮添加了背景颜色,可以看到按钮实际上已添加到导航栏,并且单击事件也正常工作。但按钮图像不可见。
如何解决?
谢谢
【问题讨论】:
-
模拟器和设备上都会出现这种情况吗?
-
为什么不使用 barbutton 数组制作自定义条形按钮。像这样 ....stackoverflow.com/questions/31612471/…
标签: ios objective-c uinavigationbar uibarbuttonitem uinavigationitem