如何在 UITabBarController 中垂直居中系统图像

Posted

技术标签:

【中文标题】如何在 UITabBarController 中垂直居中系统图像【英文标题】:How to vertically center system images in a UITabBarController 【发布时间】:2020-05-13 07:07:37 【问题描述】:

我正在尝试将新的 SFSymbols 用于标签栏中的图标。

图像应在其各自的标签栏项目中居中,但它们似乎与顶部对齐:

这是来自 View Hierarchy Debugger 的图像:

这是用于创建图像的代码:

class TabBarController: UITabBarController 
  override func viewWillAppear(_ animated: Bool) 
    super.viewWillAppear(animated)

    children[0].tabBarItem.image = UIImage(systemName: "magnifyingglass")
    children[1].tabBarItem.image = UIImage(systemName: "ellipsis")
  

【问题讨论】:

保留我假设的标签? 是的,完全正确。我想将所有图像和所有文本垂直排列 你试过这样的事情吗? ***.com/questions/16285205/… 【参考方案1】:

您可以通过指定从底部开始的基线偏移量将其垂直居中,该偏移量是您用于 SFSymbol 的字体大小的一半。在您的情况下,您没有指定字体,因此它将是默认系统字体大小的一半。

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

【讨论】:

以上是关于如何在 UITabBarController 中垂直居中系统图像的主要内容,如果未能解决你的问题,请参考以下文章

如何从 View 获取数据到 UITabBarController

如何在特定索引处显示 UITabBarController?

如何防止 UITabBarController 更新子视图?

如何在 uitabbarcontroller 中的 uibutton 单击事件上推送其他视图控制器?

如何在 UITabBarController 中获取特定 UIViewController 的索引

如何在 Swift 中制作简单的 UITabBarController?