【问题标题】:Change alpha of the first image in tabbar SWIFT更改标签栏 SWIFT 中第一张图像的 alpha
【发布时间】:2019-09-14 02:31:01
【问题描述】:

我正在使用 ESTabBarController 来构建 TabBar。我需要访问第一个 imageView 来更改它的alpha,每次它是selectedItem。顺便说一下,tabbarcontroller 在 UINavigationController 下。我能得到一些帮助吗?如果需要,我可以提供代码。我试图在didSetselectedIndex 上更改alpha

我试过了

navigationController.tabBarController.tabBar.compactMap{$0 as? UIImageView}.first.alpha = 1 这将返回 nil。

【问题讨论】:

    标签: ios swift xcode swift5


    【解决方案1】:

    ESTabBarController 实际上是您想要做的事情的完美框架,还有更多,因为您的问题,我才看了一下!所以,下面的代码肯定能达到你的预期。将其放入新的单一视图应用程序 ViewController.swift 并亲自尝试。

    您可以通过更改ExampleBasicContentView 来自定义处于选定状态的第二个按钮。如果您想让所有 TabBarItems 行为相同,请将 ExampleBasicContentView() 添加为初始化程序的第一个参数,就像我为第二个项目所做的那样。

    import UIKit
    import ESTabBarController
    
    class ViewController: ESTabBarController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            let v1 = ExampleViewController()
            let v2 = ExampleViewController()
            let v3 = ExampleViewController()
            let v4 = ExampleViewController()
            let v5 = ExampleViewController()
    
            v1.tabBarItem = ESTabBarItem.init(title: "Home", image: UIImage(named: "home"), selectedImage: UIImage(named: "home_1"))
            v2.tabBarItem = ESTabBarItem.init(ExampleBasicContentView(), title: "Find", image: UIImage(named: "find"), selectedImage: UIImage(named: "find_1"))
            v3.tabBarItem = ESTabBarItem.init(title: "Photo", image: UIImage(named: "photo"), selectedImage: UIImage(named: "photo_1"))
            v4.tabBarItem = ESTabBarItem.init(title: "Favor", image: UIImage(named: "favor"), selectedImage: UIImage(named: "favor_1"))
            v5.tabBarItem = ESTabBarItem.init(title: "Me", image: UIImage(named: "me"), selectedImage: UIImage(named: "me_1"))
    
            tabBar.shadowImage = nil
            viewControllers = [v1, v2, v3, v4, v5]
        }
    }
    
    
    class ExampleBasicContentView: ESTabBarItemContentView {
        override init(frame: CGRect) {
            super.init(frame: frame)
            highlightIconColor = iconColor.withAlphaComponent(0.4)
            highlightTextColor = textColor.withAlphaComponent(0.4)
        }
    
        public required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
    }
    
    

    【讨论】:

    • 接受了这个,因为它精确而清晰,虽然我没有使用它,因为我已经有了这些类,它只是缺少一行代码。不过我解决了。
    • 感谢您的接受,很高兴您能理解!缺少什么?
    • 基本上是你所拥有的highlightIconColor = iconColor.withAlphaComponent(0.4),但我自己的变化。
    【解决方案2】:

    可以使用以下内容:

    tabBarController?.tabBar.items?[i].image.alpha = // Your value
    

    【讨论】:

    • 图像没有成员alpha。此外,我需要 imageView 而不是图像。
    猜你喜欢
    • 2015-07-11
    • 1970-01-01
    • 2014-12-23
    • 2020-08-17
    • 2014-11-22
    • 2019-07-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多