我正在使用以下代码在TabBar项目之间添加分隔符视图。它们在iPhone中可见,但在iPad中不可见。
//Add seprators Line
if let items = self.tabBar.items {
//Get the height of the tab bar
let height = self.tabBar.bounds.height
//Calculate the size of the items
let numItems = CGFloat(items.count)
let itemSize = CGSize(
width: tabBar.frame.width / numItems,
height: tabBar.frame.height)
for (index, _) in items.enumerated() {
//We don't want a separator on the left of the first item.
if index > 0 {
//Xposition of the item
let xPosition = itemSize.width * CGFloat(index)
/* Create UI view at the Xposition,
with a width of 0.5 and height equal
to the tab bar height, and give the
view a background color
*/
let separator = UIView(frame: CGRect(
x: xPosition, y: 0, width: 0.5, height: height))
separator.backgroundColor = UIColor.blue
tabBar.insertSubview(separator, at: 1)
}
}
}
答案 0 :(得分:1)
有效。这就是我用过的:
self.tabBar.itemPositioning = UITabBarItemPositioning.fill
答案 1 :(得分:0)
# Here is a Perfect Solution #
override func viewDidLoad() {
super.viewDidLoad()
self.tabBar.itemPositioning = .fill
ChangeOrientation(width: self.tabBar.bounds.width)
}
override func viewWillTransition(to size: CGSize, with coordinator :UIViewControllerTransitionCoordinator) {
super.viewWillTransition(to: size, with: coordinator)
removeSperator()
ChangeOrientation(width: size.width)
}
func removeSperator() {
if let items = self.tabBar.items {
for (index, _) in items.enumerated() {
//We don't want a separator on the left of the first item.
if index > 0 {
if let viewWithTag = tabBar.viewWithTag(index) {
//print("index for remove : \(index)")
viewWithTag.removeFromSuperview()
}
else {
print("tag not found")
}
}
}
}
}
func ChangeOrientation(width : CGFloat) {
if let items = self.tabBar.items {
//Get the height of the tab bar
let height = self.tabBar.bounds.height
//Calculate the size of the items
let numItems = CGFloat(items.count)
let itemSize = CGSize(
width: width / numItems,
height: tabBar.frame.height)
for (index, _) in items.enumerated() {
//We don't want a separator on the left of the first item.
if index > 0 {
//Xposition of the item
let xPosition = itemSize.width * CGFloat(index)
/* Create UI view at the Xposition,
with a width of 0.5 and height equal
to the tab bar height, and give the
view a background color
*/
//print("index : \(index)")
let separator = UIView(frame: CGRect(
x: xPosition, y: 0, width: 0.5, height: height))
separator.tag = index
separator.backgroundColor = UIColor.white
tabBar.insertSubview(separator, at: 1)
}
}
}
}
答案 2 :(得分:0)
使用以 SWIFT 5.0
编写的此功能fileprivate func addSeparatorToTabBar() {
if let items = self.tabBarController?.tabBar.items {
//Get the height of the tab bar
let height = (self.tabBarController?.tabBar.bounds)!.height
//Calculate the size of the items
let numItems = CGFloat(items.count)
let itemSize = CGSize(
width: (self.tabBarController?.tabBar.frame.width)! / numItems,
height: (self.tabBarController?.tabBar.frame.height)!)
for (index, _) in items.enumerated() {
//We don't want a separator on the left of the first item.
if index > 0 {
//Xposition of the item
let xPosition = itemSize.width * CGFloat(index)
/* Create UI view at the Xposition,
with a width of 0.5 and height equal
to the tab bar height, and give the
view a background color
*/
let separator = UIView(frame: CGRect(
x: xPosition, y: 0, width: 0.5, height: height))
separator.backgroundColor = UIColor.gray
self.tabBarController?.tabBar.insertSubview(separator, at: 1)
}
}
}
}
并在 viewDidLoad()
中调用它addSeparatorToTabBar()