gpt4 book ai didi

ios - UITabBar 个性化、颜色和分隔线

转载 作者:行者123 更新时间:2023-11-28 15:26:28 25 4
gpt4 key购买 nike

我正在制作这个具有标签栏的 iOS 应用程序 (Swift 3),但我没有找到一种方法来按照我想要的方式对其进行个性化设置。我想在每个图标之间添加线条,并且我希望它在被选中时显示为红色,如下所示:

UITabBar

最佳答案

自定义一个继承自UITabBarController的类,然后将其作为你的TabBarController的类。这里提供的主要程序:

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 - UITabBar 个性化、颜色和分隔线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45156035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com