简体   繁体   中英

IOS - tab bar item image modifications

I want to change the default color of the tab bar item image to be the original color of the image (black) as opposed to a gray when it's not selected.

I also want to change the tab bar item image to a filled version once it's selected.

Last thing is the position.. It seems to expect text under it so it's not centered, how do I center it vertically and possibly make it smaller?

I'm currently setting it this way:

   let profileNavController = UINavigationController(rootViewController: profileController)
        profileNavController.tabBarItem.image = UIImage(named: "icon_tab_user")

This is how it looks selected and unselected: 在此处输入图片说明 在此处输入图片说明

From apple we know,

By default, the actual unselected and selected images are automatically created from the alpha values in the source images. To prevent system coloring, provide images with UIImageRenderingModeAlwaysOriginal.

Take a look here.
Changing tab bar item image and text color iOS

I was able to change the highlighted and unhighlighted images as well as the position with this:

let profileNavController = UINavigationController(rootViewController: profileController)
        let profileTabBarItem  = UITabBarItem(title: nil, image: UIImage(named: "icon_tab_user")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal), selectedImage: UIImage(named: "icon_tab_user_highlighted"))
        profileTabBarItem.imageInsets = UIEdgeInsetsMake(5.5, 0, -5.5, 0)
        profileNavController.tabBarItem = profileTabBarItem

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM