我想在我的标签栏中对齐标签栏项目图像。我知道我可以在 IB 中做到这一点,我做到了,而且它奏效了。不过我想以编程方式完成它。我正在尝试在 AppDelegate 中执行此操作。以下是我的代码,它不起作用。 谁能指出我做错了什么?
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "RootTabBarController") as! UITabBarController
let tabArray = tabBarController.tabBar.items as NSArray?
let homeTabItem = tabArray?.object(at: 0) as! UITabBarItem
homeTabItem.imageInsets = UIEdgeInsetsMake(12.0, 0.0, -12.0, 0.0)
最佳答案
访问 tabbaritem View ,现在根据您的需要进行更改。例如,我需要设置图像的高度宽度所以我在下面做了:
class Tabbar: UITabBarController,UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
self.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(true)
// self.tabBarController?.selectedIndex = 2
for tabBarItem in (self.tabBar.items)!{
let viewTabBar = tabBarItem.value(forKey: "view") as? UIView
let imgView = viewTabBar?.subviews[0] as? UIImageView
viewTabBar?.origin.y = 6
imgView?.frame.size.height = 24
imgView?.frame.size.width = 24
imgView?.clipsToBounds = true
imgView?.contentMode = .scaleAspectFit
}
}
override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
for tabBarItem in (self.tabBar.items)!{
let viewTabBar = tabBarItem.value(forKey: "view") as? UIView
let imgView = viewTabBar?.subviews[0] as? UIImageView
imgView?.frame.size.height = 24
imgView?.frame.size.width = 24
imgView?.clipsToBounds = true
imgView?.contentMode = .scaleAspectFit
}
}
}
关于ios - 通过访问 AppDelegate 中的 Tab Bar Items 更改 Tab Bar Item Image inset,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50359988/