带有图像和动态文本的UITool条形按钮"低于"它

时间:2014-05-20 11:34:43

标签: ios ipad ios7

我想在UIToolbar上实现一个按钮,其中“图像”位于“顶部”和“文本”下方,如下所示。

enter image description here

我使用了以下代码:

UIButton* customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setImage:[UIImage imageNamed:@"search2.png"] forState:UIControlStateNormal];
[customButton setTitle:@"Categorie" forState:UIControlStateNormal];
[customButton sizeToFit];
UIBarButtonItem* cataBtn = [[UIBarButtonItem alloc] initWithCustomView:customButton];initWithCustomView:customButton];
cataBtn 

但它显示的结果如下:

enter image description here

我应该如何以图像位于顶部和文本位于其下方的方式进行自定义?

我不想使用包含图片和文字的单个图片,因为文字是动态变化的。

1 个答案:

答案 0 :(得分:1)

您需要设置边缘插入

customButton.titleEdgeInsets = UIEdgeInsetsMake(0, -60, -30, 0);
customButton.imageEdgeInsets = UIEdgeInsetsMake(-15, 0 , 0, 0); // give some space for text