8.17 TabBarItem 标签栏纯代码

创建几个类,用来之后显示标签栏上的按钮,并改下背景色

8.17 TabBarItem 标签栏纯代码_第1张图片
Paste_Image.png
import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.makeKeyAndVisible()
        
        let firstCtrl = ViewController()
        
        //设置导航控制器
        let navCtrl = UINavigationController(rootViewController: firstCtrl)
        navCtrl.tabBarItem.title = "第一页"
//        navCtrl.tabBarItem.image = UIImage(named: "1.png")?.imageWithRenderingMode(.AlwaysOriginal)
        
        //设置图标背景图,默认蓝色,改为原始颜色显示
        firstCtrl.tabBarItem.image = UIImage(named: "1.png")?.imageWithRenderingMode(.AlwaysOriginal)
        //        firstCtrl.tabBarItem.selectedImage   选中后图片
        firstCtrl.tabBarItem.badgeValue = "5"
        firstCtrl.tabBarItem.title = "First"
        
        let secondCtrl = Second()
        secondCtrl.tabBarItem.title = "Second"
        
        let thirdCtrl = Third()
        thirdCtrl.tabBarItem.title = "Third"
        
        let fourthCtrl = Fourth()
        fourthCtrl.tabBarItem.title = "Fourth"
        
        let fivthCtrl = Fivth()
        fivthCtrl.tabBarItem.title = "Fivth"

        let tabCtrl = UITabBarController()
        tabCtrl.viewControllers = [navCtrl,secondCtrl,thirdCtrl,fourthCtrl,fivthCtrl]
        self.window?.rootViewController = tabCtrl
        
        return true
    }

在 ViewContrller中编辑导航栏属性,我们给它设置点击屏幕跳转到第二个页面的功能。

编译运行,如下图:

8.17 TabBarItem 标签栏纯代码_第2张图片
Paste_Image.png
8.17 TabBarItem 标签栏纯代码_第3张图片
Paste_Image.png
8.17 TabBarItem 标签栏纯代码_第4张图片
Paste_Image.png
8.17 TabBarItem 标签栏纯代码_第5张图片
Paste_Image.png
8.17 TabBarItem 标签栏纯代码_第6张图片
Paste_Image.png

可以直接点击标签栏上的按钮,直接跳转到对应的界面。

你可能感兴趣的:(8.17 TabBarItem 标签栏纯代码)