Swift5.0 创建UITabBarController

直接上代码,如何创建一个UITabBarController

import UIKit

class MYTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        addChildVC(childVC: HomeViewController(), tile1: "首页", image1: "home")
        addChildVC(childVC: HomeViewController(), tile1: "消息", image1: "message")
        addChildVC(childVC: HomeViewController(), tile1: "发现", image1: "find")
        addChildVC(childVC: HomeViewController(), tile1: "我的", image1: "me")
    }
    
    func addChildVC(childVC:UIViewController,tile1:String,image1:String) -> Void {
        childVC.title = tile1
        
        var img = UIImage(named: image1)
        img = img?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        
        var selectedImg = UIImage(named: image1 + "_selected")
        selectedImg = selectedImg?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)
        
        childVC.tabBarItem.image = img
        childVC.tabBarItem.selectedImage = selectedImg
        let nav = UINavigationController(rootViewController: childVC)
        addChild(nav)
    }
}

注意:

img = img?.withRenderingMode(UIImage.RenderingMode.alwaysOriginal)

图片需要使用这种模式才能显示出来


Swift5.0 创建UITabBarController_第1张图片
image.png

你可能感兴趣的:(Swift5.0 创建UITabBarController)