【发布时间】:2017-11-12 14:41:44
【问题描述】:
我使用的是 UITabBar,而不是 UITabBarController。每当用户单击任何选项卡时,我都能看到带下划线的图像。
public override func viewDidLoad() {
super.viewDidLoad()
UITabBar.appearance().selectionIndicatorImage = getImageWithColorPosition(color: UIColor.darkGray, size: CGSize(width:presenterView.frame.size.width/2, height:49), lineSize: CGSize(width:presenterView.frame.size.width/2, height:2))
}
//getImageWithColorPosition用于在UITabBarItem下方添加下划线的图片
func getImageWithColorPosition(color: UIColor, size: CGSize, lineSize: CGSize) -> UIImage {
let rect = CGRect(x: 0, y: 0, width: size.width, height: size.height)
let rectLine = CGRect(x: 0, y: size.height-lineSize.height, width: lineSize.width, height: lineSize.height)
UIGraphicsBeginImageContextWithOptions(size, false, 0)
UIColor.clear.setFill()
UIRectFill(rect)
color.setFill()
UIRectFill(rectLine)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
public override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
openItem1()
}
public func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
if (item.tag == 0) {
openItem1()
} else {
openItem2()
}
}
但是,当用户单击任何选项卡项时,只会显示带下划线的图像。当用户第一次登陆屏幕时,我想选择一个默认选项卡。当我以编程方式执行此操作时,下划线图像不会出现。我阅读了几篇堆栈溢出帖子,其中说 UITabBar 上的选择不能以编程方式触发。
还有其他方法吗?
【问题讨论】:
标签: swift uiviewcontroller uitabbar uitabbaritem