我在下面的代码中创建了两个UIBarButtonItem
。后来我将它们添加到rightBarButtonItems
但是只显示了两个按钮中的一个(我在列表中首先放置的那个)。
我想也许标题的视图(未设置)将位于rightBarButtonItems的前面,因为Apple文档说“如果没有足够的空间来显示数组中的所有项目,那些会重叠的项目标题视图(如果存在)或栏左侧的按钮不会显示。“
但这似乎也不是,因为视图的框架= {0,0} {0,0}
我无法弄清楚我做错了什么。任何人都可以告诉我如何让所有正确的BarButtonItems显示?代码:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareAction:)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:@"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *wishlistButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItems:[[NSArray alloc] initWithObjects:shareButton, wishlistButton, nil]];
答案 0 :(得分:0)
使用UIBarButtonItem
:
UIBarButtonItem *button = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"custom-bar-button.png"] style:UIBarButtonItemStylePlain target:self action:@selector(barButtonTapped:)];
而不是:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:@"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];