UITabBar 个性化、颜色和分隔线
UITabBar personalization, color and separator lines
我正在制作这个 iOS 应用程序 (Swift 3),它有一个标签栏,但我没有找到一种方法来按照我想要的方式对其进行个性化设置。我想在每个图标之间添加线条,并且我希望它在被选中时显示为红色,如下所示:
自定义一个class继承自UITabBarController,然后将其作为你的TabBarController的class。这里提供的主要程序:
import UIKit
class MainTabBarController: UITabBarController,UITabBarControllerDelegate {
var firstBackgroundView:UIView!
//var secondBackgroundView:UIView!
//......
override func viewDidLoad() {
super.viewDidLoad()
//Lines:
let topline = CALayer()
topline.frame = CGRect(x: 0, y: 0, width: self.tabBar.frame.width, height: 2)
topline.backgroundColor = UIColor.gray.cgColor
self.tabBar.layer.addSublayer(topline)
let firstVerticalLine = CALayer()
firstVerticalLine.frame = CGRect(x: self.tabBar.frame.width / 5, y: 0, width: 2, height: self.tabBar.frame.height)
firstVerticalLine.backgroundColor = UIColor.gray.cgColor
self.tabBar.layer.addSublayer(firstVerticalLine)
//Continue to add other lines to divide tab items...
//......
//Background views
firstBackgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.tabBar.frame.width / 5, height: self.tabBar.frame.height))
firstBackgroundView.backgroundColor = UIColor.red
firstBackgroundView.isHidden = false //true for others.
self.tabBar.addSubview(firstBackgroundView)
self.tabBar.sendSubview(toBack: firstBackgroundView)
//Continue to add other background views for each tab item...
//......
self.delegate = self
}
public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if self.selectedIndex == 0 {
firstBackgroundView.isHidden = false
//othersBackgroundView.isHidden = true
}
else if self.selectedIndex == 1 {
//......
}
}
}
我正在制作这个 iOS 应用程序 (Swift 3),它有一个标签栏,但我没有找到一种方法来按照我想要的方式对其进行个性化设置。我想在每个图标之间添加线条,并且我希望它在被选中时显示为红色,如下所示:
自定义一个class继承自UITabBarController,然后将其作为你的TabBarController的class。这里提供的主要程序:
import UIKit
class MainTabBarController: UITabBarController,UITabBarControllerDelegate {
var firstBackgroundView:UIView!
//var secondBackgroundView:UIView!
//......
override func viewDidLoad() {
super.viewDidLoad()
//Lines:
let topline = CALayer()
topline.frame = CGRect(x: 0, y: 0, width: self.tabBar.frame.width, height: 2)
topline.backgroundColor = UIColor.gray.cgColor
self.tabBar.layer.addSublayer(topline)
let firstVerticalLine = CALayer()
firstVerticalLine.frame = CGRect(x: self.tabBar.frame.width / 5, y: 0, width: 2, height: self.tabBar.frame.height)
firstVerticalLine.backgroundColor = UIColor.gray.cgColor
self.tabBar.layer.addSublayer(firstVerticalLine)
//Continue to add other lines to divide tab items...
//......
//Background views
firstBackgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.tabBar.frame.width / 5, height: self.tabBar.frame.height))
firstBackgroundView.backgroundColor = UIColor.red
firstBackgroundView.isHidden = false //true for others.
self.tabBar.addSubview(firstBackgroundView)
self.tabBar.sendSubview(toBack: firstBackgroundView)
//Continue to add other background views for each tab item...
//......
self.delegate = self
}
public func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if self.selectedIndex == 0 {
firstBackgroundView.isHidden = false
//othersBackgroundView.isHidden = true
}
else if self.selectedIndex == 1 {
//......
}
}
}