How to create tabbar with ESTabBarController in swift 3.0

前端之家收集整理的这篇文章主要介绍了How to create tabbar with ESTabBarController in swift 3.0前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

How to create tabbar with ESTabBarController in swift 3.0

First Install CocoaPods

  1. Open terminal and cd ~ to your project directory
  2. Run the command - pod init
  3. Your podfile should be use with - pod "ESTabBarController-swift" and save it
  4. And install it with command pod install

Open project file of .xcworkspace extension

  1. In project we need to add Content all swift class and pop.framework
  2. Don't add pop.framework using add File to. you must be add from Framework and add Others.
  3. In Content folder's all file import ESTabBarController_swift

StoryBord Stuff

Add navigation Controller ane also add ExampleNavigationController from the example code of EST demo. (You can add your own too) but make sure you set its Class of navigation custom swift class.
Code Stuff at AppDelegate.swift

You need to do following code AppDelegate.swift

//
//  AppDelegate.swift
//  DNApp
//
//  Created by 雷神 on 2017/9/6.
//  Copyright © 2017年 aider.cc. All rights reserved.
//

import UIKit
import CoreData
import ESTabBarController_swift

@UIApplicationMain
class AppDelegate: UIResponder,UIApplicationDelegate,UITabBarControllerDelegate {

    
    var window: UIWindow?
    
    
    func application(_ application: UIApplication,didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        
        let tabBarController = ESTabBarController()
        tabBarController.delegate = self
        tabBarController.title = "Irregularity"
        tabBarController.tabBar.shadowImage = UIImage(named: "transparent")
        tabBarController.tabBar.backgroundImage = UIImage(named: "background_dark")
        tabBarController.shouldHijackHandler = {
            tabbarController,viewController,index in
            if index == 2 {
                return true
            }
            return false
        }
        tabBarController.didHijackHandler = {
            [weak tabBarController] tabbarController,index in
            
            DispatchQueue.main.asyncAfter(deadline: .now() + 0.2) {
                let alertController = UIAlertController.init(title: nil,message: nil,preferredStyle: .actionSheet)
                let takePhotoAction = UIAlertAction(title: "Take a photo",style: .default,handler: nil)
                alertController.addAction(takePhotoAction)
                let selectFromAlbumAction = UIAlertAction(title: "Select from album",handler: nil)
                alertController.addAction(selectFromAlbumAction)
                let cancelAction = UIAlertAction(title: "Cancel",style: .cancel,handler: nil)
                alertController.addAction(cancelAction)
                tabBarController?.present(alertController,animated: true,completion: nil)
            }
        }

        let v1 = ExampleViewController()
        let v2 = ExampleViewController()
        let v3 = ExampleViewController()
        let v4 = ExampleViewController()
        let v5 = ExampleViewController()
        v1.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(),title: "Home",image: UIImage(named: "home"),selectedImage: UIImage(named: "home_1"))
        v2.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(),title: "Find",image: UIImage(named: "find"),selectedImage: UIImage(named: "find_1"))
        v3.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(),title: "Photo",image: UIImage(named: "photo"),selectedImage: UIImage(named: "photo_1"))
        v4.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(),title: "Favor",image: UIImage(named: "favor"),selectedImage: UIImage(named: "favor_1"))
        v5.tabBarItem = ESTabBarItem.init(ExampleBouncesContentView(),title: "Me",image: UIImage(named: "me"),selectedImage: UIImage(named: "me_1"))
        
        tabBarController.viewControllers = [v1,v2,v3,v4,v5]

        let navigationController = ExampleNavigationController.init(rootViewController: tabBarController)
        tabBarController.title = "Example"
        
        
        self.window?.rootViewController = navigationController

        return true
    }
    
    func applicationWillResignActive(_ application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks,disable timers,and invalidate graphics rendering callbacks. Games should use this method to pause the game.
    }
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources,save user data,invalidate timers,and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution,this method is called instead of applicationWillTerminate: when the user quits.
    }
    
    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }
    
    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was prevIoUsly in the background,optionally refresh the user interface.
    }
    
    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
        // Saves changes in the application's managed object context before the application terminates.
        self.saveContext()
    }
    
    // MARK: - Core Data stack
    
    lazy var persistentContainer: NSPersistentContainer = {
        /*
         The persistent container for the application. This implementation
         creates and returns a container,having loaded the store for the
         application to it. This property is optional since there are legitimate
         error conditions that could cause the creation of the store to fail.
         */
        let container = NSPersistentContainer(name: "DNApp")
        container.loadPersistentStores(completionHandler: { (storeDescription,error) in
            if let error = error as NSError? {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application,although it may be useful during development.
                
                /*
                 Typical reasons for an error here include:
                 * The parent directory does not exist,cannot be created,or disallows writing.
                 * The persistent store is not accessible,due to permissions or data protection when the device is locked.
                 * The device is out of space.
                 * The store could not be migrated to the current model version.
                 Check the error message to determine what the actual problem was.
                 */
                fatalError("Unresolved error \(error),\(error.userInfo)")
            }
        })
        return container
    }()
    
    // MARK: - Core Data Saving support
    
    func saveContext () {
        let context = persistentContainer.viewContext
        if context.hasChanges {
            do {
                try context.save()
            } catch {
                // Replace this implementation with code to handle the error appropriately.
                // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application,although it may be useful during development.
                let nserror = error as NSError
                fatalError("Unresolved error \(nserror),\(nserror.userInfo)")
            }
        }
    }
    
}
原文链接:https://www.f2er.com/swift/321169.html

猜你在找的Swift相关文章