如何在 UITabBarItem 中垂直居中 SF Symbols 图像? [英] How to center a SF Symbols image vertically in UITabBarItem?

查看:29
本文介绍了如何在 UITabBarItem 中垂直居中 SF Symbols 图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的 iOS 应用程序中使用 SF 符号图像作为标签图像,方法如下:

I am using SF Symbols images as tab images in my iOS app by assigning them as follows:

self.tabBarItem.image = UIImage(systemName: "ellipsis")  

这会导致所有图像顶部对齐,但我希望它们垂直居中.

This results in all images being top-aligned, but I would like to have them centered vertically.

我该怎么做?

推荐答案

显然 SF 符号默认以系统字体大小呈现.因此,如果您向省略号符号添加一半大小的基线偏移量,您几乎可以完全以这种方式将其垂直居中.

Apparently SF symbols are rendered with system font size by default. So if you add a baseline offset of half that size to the ellipsis symbol you could almost perfectly center it vertically that way.

这几乎是完美的,因为省略号有自己的高度,这个解决方案没有考虑到这个高度,即使它不多.

It's only almost perfect because ellipsis symbol has a height of its own which is not accounted for by this solution, even if it is not much.

self.tabBarItem.image = UIImage(systemName: "ellipsis")!.withBaselineOffset(fromBottom: UIFont.systemFontSize / 2)

这篇关于如何在 UITabBarItem 中垂直居中 SF Symbols 图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆