首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择时更改UITabBarItem图像(Xcode12,Swift)

选择时更改UITabBarItem图像(Xcode12,Swift)
EN

Stack Overflow用户
提问于 2020-12-14 00:29:09
回答 3查看 58关注 0票数 1

在UITabBarDelegate的tabBar(_:didSelect:)函数中,我尝试切换tabBarItem的图像。这适用于iOS 14,但当我使用iOS 11进行测试时,它不会更改图像。

我尝试包装在DispatchQueue.main.async {}中并调用tabBar.setNeedsDisplay()和tabBar.setNeedsLayout(),但这没有帮助。

代码语言:javascript
复制
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    switch item.tag {
        case 0:
            if publishAudio == false {
                item.image = UIImage(named: "mute_off")
            } else {
                item.image = UIImage(named: "mute_on")
            }   
        default:
            preconditionFailure("Unexpected item tag: \(item.tag)")
    }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-12-14 05:31:38

弄清楚了,在设置图像时,设置imageselectedImage

代码语言:javascript
复制
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
    switch item.tag {
        case 0:
            if publishAudio == false {
                item.image = UIImage(named: "mute_off")
                item.selectedImage = UIImage(named: "mute_off")
            } else {
                item.image = UIImage(named: "mute_on")
                item.selectedImage = UIImage(named: "mute_on")
            }   
        default:
            preconditionFailure("Unexpected item tag: \(item.tag)")
    }
}
票数 0
EN

Stack Overflow用户

发布于 2020-12-14 00:32:37

您可以使用每个viewController的UITabBar图像从情节提要中创建它,如下图所示:

票数 1
EN

Stack Overflow用户

发布于 2020-12-21 05:16:45

试试这个:

代码语言:javascript
复制
   self.tabBarController?.tabBar.items![0].image = UIImage(named: "your image name")
    // items![0] index of your tab bar item.items![0] means tabbar first item

    self.tabBarController?.tabBar.items![0].selectedImage = UIImage(named: "your image name")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65277942

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档