最终为 UIBarButtonItems 创建了自定义视图。这不是很好,但它会暂时起作用。我只是将旧的 UIBarButtonItems 传递给该函数,该函数返回一个新的。
注意:如果有标题,我只是向上移动按钮的图像并在下面添加一个简单的标签,实际上并没有弄乱 titleEdgeInsets 和 centering 。另外,我将宽度设置为默认的 56。
-(UIBarButtonItem *)convertToButton:(UIBarButtonItem *)original
{
if ( SYSTEM_VERSION_LESS_THAN(@"7.0"))
return original;
CGFloat textFieldHeight = 13.f;
UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 56, self.toolbar.frame.size.height);
button.showsTouchWhenHighlighted = YES;
[button setImage:original.image forState:UIControlStateNormal];
[button addTarget:original.target action:original.action forControlEvents:UIControlEventTouchUpInside];
button.tag = original.tag;
UIView * wr = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 56, self.toolbar.frame.size.height)] autorelease];
[wr addSubview:button];
wr.backgroundColor = [UIColor clearColor];
if (original.title != nil)
{
button.imageEdgeInsets = UIEdgeInsetsMake(-7, 0, 0, 0);
UILabel * l = [[[UILabel alloc] initWithFrame:CGRectMake(0, self.toolbar.frame.size.height - textFieldHeight, 56, textFieldHeight)] autorelease];
l.font = [UIFont systemFontOfSize:11.f];
l.backgroundColor = [UIColor clearColor];
l.textColor = [UIColor lightGrayColor];
l.textAlignment = UITextAlignmentCenter;
l.text = original.title;
[wr addSubview:l];
}
UIBarButtonItem * theNew = [[[UIBarButtonItem alloc] initWithCustomView:wr] autorelease];
theNew.tag = original.tag;
theNew.width = 56;
return theNew;
}