【问题标题】:Tab bar item with two images带有两个图像的标签栏项目
【发布时间】:2021-04-11 11:41:38
【问题描述】:
【问题讨论】:
标签:
ios
swift
xcode
interface-builder
【解决方案1】:
也许这会对那里的人有所帮助,所以这就是我实现这种外观的方法。
我使用盾牌图像作为选项卡图标,然后在 TabBarController.swift 中的 tabBar 顶部添加图像,如下所示:
let button = UIButton(type: .custom)
button.frame = CGRect(x: 0.0, y: 0.0, width: 45, height: 45)
button.center = tabBar.center
button.sd_setBackgroundImage(with: URL(string: UserPreferencesManager.getFavoriteTeamImage()), for: .normal, completed: nil)
button.sd_setBackgroundImage(with: URL(string: UserPreferencesManager.getFavoriteTeamImage()), for: .highlighted, completed: nil)
button.addTarget(self, action: #selector(btnTouched), for:.touchUpInside)
view.addSubview(button)
并使用 btnTouched 函数:
@objc func btnTouched(){
self.selectedIndex = 2
}
将图像放在 tabBar 的顶部会让人产生成为中心项目一部分的错觉,但是当用户触摸该选项卡时会妨碍执行该选项卡应该执行的操作,因此 btnTouched 函数。