UIBarButtonItems 的水平定位没有问题,我可以简单地用固定/灵活的空间项填充空间。但是,我似乎无法垂直调整工具栏项目。UIToolbar 没有对齐属性,UIBarButtonItem 没有办法设置它的框架。
我需要这样做,因为我们混合使用了使用 initWithImage 创建的自定义图标和使用 initWithBarButtonSystemItem 创建的标准图标。自定义图标没有正确居中(它们向上偏移,相对于正确居中的系统图标),因此工具栏看起来很尴尬。
UIBarButtonItems 的水平定位没有问题,我可以简单地用固定/灵活的空间项填充空间。但是,我似乎无法垂直调整工具栏项目。UIToolbar 没有对齐属性,UIBarButtonItem 没有办法设置它的框架。
我需要这样做,因为我们混合使用了使用 initWithImage 创建的自定义图标和使用 initWithBarButtonSystemItem 创建的标准图标。自定义图标没有正确居中(它们向上偏移,相对于正确居中的系统图标),因此工具栏看起来很尴尬。
是的,imageInsets 对我来说很好用!
float topInset = 4.0f;
myUIBarButton.imageInsets = UIEdgeInsetsMake(topInset, 0.0f, -topInset, 0.0f);
将图像向下移动 4 个像素。
避免这种情况:
float topInset = 4.0f;
myUIBarButton.imageInsets = UIEdgeInsetsMake(topInset, 0.0f, 0.0f, 0.0f);
将图像向下移动 4 个像素(但会重新调整图像高度,使其看起来被压缩)。
UIBarItem 的 imageInsets 属性?
这似乎不是任何简单、干净的方法,所以我采用了一个丑陋但实用的技巧:嵌套。我将另一个包含按钮的 UIToolbar 插入到 UIView 中,我使用 initWithCustomView 在原始工具栏上将其设置为 UIBarButtonItem。第二个 UIToolbar 可以在 UIView 内自由移动,实际图标保留了一个 UIBarButtonItem 的所有属性。
啊。