xcode UITabBarController未安装到iOS Swift?的屏幕上

sqougxex  于 2022-11-18  发布在  iOS
关注(0)|答案(1)|浏览(130)

我已经以编程方式实现了UITabBarController。功能工作正常,但UITabBarController不适合屏幕。
下面是我代码:

class ViewController: UIViewController {

let tabBarCnt = UITabBarController()

override func viewDidLoad() {
    super.viewDidLoad()

    tabBarCnt.tabBar.tintColor = UIColor.black
    
    createTabBarController()
}

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()
    addHeightConstraintToTabbar()
}

func addHeightConstraintToTabbar() -> Void {
    let heightConstant:CGFloat = self.view.safeAreaInsets.bottom + 49.0
    tabBarCnt.tabBar.heightAnchor.constraint(equalToConstant: heightConstant).isActive = true
}

  func createTabBarController() {

    let firstVc = UIViewController()
    firstVc.title = "First"
    firstVc.view.backgroundColor =  UIColor.red
    firstVc.tabBarItem = UITabBarItem.init(title: "Home", image: UIImage(named: "HomeTab"), tag: 0)

    let secondVc = UIViewController()
    secondVc.title = "Second"
    secondVc.view.backgroundColor =  UIColor.green
    secondVc.tabBarItem = UITabBarItem.init(title: "Location", image: UIImage(named: "Location"), tag: 1)

    let controllerArray = [firstVc, secondVc]
    tabBarCnt.viewControllers = controllerArray.map{ UINavigationController.init(rootViewController: $0)}

    self.view.addSubview(tabBarCnt.view)
 }

}

结果屏幕截图

xytpbqjk

xytpbqjk1#

添加此行:

tabBarCnt.additionalSafeAreaInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

相关问题