ios - 通过访问 AppDelegate 中的 Tab Bar Items 更改 Tab Bar Item Image inset

标签 ios storyboard tabbar uitabbaritem swift4.1

我想在我的标签栏中对齐标签栏项目图像。我知道我可以在 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/

相关文章:

iphone - 当我只有 IPA 文件时,如何制作应用程序的演示视频?

ios - ZXing 扫描多个条码

ios - 解析查询花费的时间太长

ios - 无法导航到 IOS Storyboard中的目标 View Controller

flutter - 如何在 SliverAppBar 中仅显示选项卡 View

android - 如何在屏幕中心 float 创建标签栏?

ios - UIImageView+animatedGIF总是LOOPS

iphone - UIWindow 在哪里实例化,storyboard 传入?

ios - 当通过 segue 返回时,选项卡栏会从 viewController 中消失 - swift

ios - FooterView 未显示在 Storyboard UICollectionView 中