0

I'm trying to use the new SFSymbols for icons in my tab bar.

The images should be centered in their respective tab bar item, but they appear to be aligned to the top:

The tab bar item is top aligned...

Here's an image from the View Hierarchy Debugger:

...as can be seen in the View Hierarchy Debugger

This is the code that is used to create the images:

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")
  }
}
rbaldwin
  • 4,581
  • 27
  • 38
Xavier Lowmiller
  • 1,381
  • 1
  • 15
  • 24

1 Answers1

0

You can centre it vertically by specifying a baseline offset from the bottom, which is half the size of the font that you are using for the SFSymbol. In your case you are not specifying a font so it will be half the size of the default system font.

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

enter image description here

rbaldwin
  • 4,581
  • 27
  • 38