iOS Swift Tab Bar Icon Insets Being Erased At Runtime

半世苍凉 提交于 2019-12-12 14:17:04

问题


I have two ViewControllers inside a tab bar controller. In interface builder, I've applied image insets (5, 5, 5, 5) to the tab bar icons to get their sizes how I want them. Everything looks great when I initially run the simulator, but when I tab to the second ViewController, both tab bar icons automatically resize to the default (0, 0, 0, 0). When I tab back to return to the first ViewController, both icons remain trapped at default size with no insets.

Note: My icon images are .pdf files in XCassets folder (set as single vector Scale Factor). I've seen other threads which suggest "balancing" the insets (5 for top, -5 for bottom). But this would COMPRESS the image by 5 away from the top and STRETCH it 5 toward the bottom. I don't see how that's meant to balance the image size.

If anyone knows what code will set the insets at runtime, maybe I could use that instead of setting insets in interface builder? Thanks in advance.


回答1:


Reading the documentation, UIBarItem (superclass of UITabBarItem) has the property imageInset, and you can access the tab bar items via self.tabBarController.tabBar.items.

Without testing it myself, I think you may have an issue with the insets getting reset when the shading changes (because of selection).

Thus, you may find that every time you change selection (and that can be detected via delegate I think), you'll need to reset the insets to the desired size.

Alternatively, you might find that just customizing the source image to the desired final appearance may be easier.



来源:https://stackoverflow.com/questions/37504048/ios-swift-tab-bar-icon-insets-being-erased-at-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!