UITabBar Button Center Vertical Alignment Swift

Viewed 4279

Here is what I am trying to do:

Screen Shot

The screenshot is taken from an earlier version of iOS device.In which show UITabBar button are not in center alignment.

Below you can check my current tabbar

Screen Shot

This is my UITabBar button add through Main. StoryBoard.

How to set all UITabBar button in vertical center alignment ?

3 Answers

You also set image insect through Main.StoryBoard:

Use this for every UITabBarItem in your tabBar. This will make your image vertically center.

tabOne.tabBarItem.imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)

Updated with example:

    let nvOneViewController = MYRNavigationController()
    let tabOne = MYRHomeViewController()
    let tabOneBarItem = UITabBarItem(title: "", image: UIImage(named: "icnHome"), selectedImage: UIImage(named: "icnHomeOn"))
    tabOne.tabBarItem = tabOneBarItem
    tabOne.tabBarItem.imageInsets = UIEdgeInsets.init(top: 5,left: 0,bottom: -5,right: 0)
    nvOneViewController.setViewControllers([tabOne], animated: false)

On iOS 13 the icons seem to be vertically centred without any custom code. Prior to iOS 13 I had to add the image insets as mentioned by @Vicky_Vignesh

Related