前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Swift -标签页控制器(UITabBarController)用法

Swift -标签页控制器(UITabBarController)用法

作者头像
Python疯子
发布2018-09-06 16:04:19
2.9K1
发布2018-09-06 16:04:19
举报
文章被收录于专栏:Python疯子Python疯子

屏幕快照 2016-11-08 19.12.46.png

Simulator Screen Shot 2016年11月8日 19.11.36.png

首先创建三个Swift(HomeViewController.swift,CenterViewController.swift,MoreViewController.swift)用于TabBar的显示 1、接下来只需要在 AppDelegate.swift 文件里写code

代码语言:javascript
复制
import UIKit
 @UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?
// 创建TabBar数组
var tabs = ["首页", "个人中心", "更多"]
var images = ["aa", "bb", "cc"]
var selectedImages = ["eee", "fff", "ggg"]

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    
    self.window = UIWindow.init(frame: UIScreen.main.bounds)
    self.window?.backgroundColor = UIColor.white
    
    let home = HomeViewController()
    let HomeNC = UINavigationController.init(rootViewController: home)
    
    // 改变图片 保证图片不失真
    let homeImage = UIImage(named:images[0])?.withRenderingMode(.alwaysOriginal)
    let homeSelectImage = UIImage(named:selectedImages[0])?.withRenderingMode(.alwaysOriginal)
    
    HomeNC.tabBarItem = UITabBarItem.init(title: "首页", image: homeImage, selectedImage: homeSelectImage)
    
    
    let center = CenterViewController()
    let CenterNC = UINavigationController.init(rootViewController: center)
    
    // 改变图片 保证图片不失真
    let centerImage = UIImage(named:images[1])?.withRenderingMode(.alwaysOriginal)
    let centerSelectImage = UIImage(named:selectedImages[1])?.withRenderingMode(.alwaysOriginal)
    
    CenterNC.tabBarItem = UITabBarItem.init(title: "个人中心", image: centerImage, selectedImage: centerSelectImage)
    // 下表数值显示
    CenterNC.tabBarItem.badgeValue = "10"
    
    
    
    let more = MoreViewController()
    let MoreNC = UINavigationController.init(rootViewController: more)
    
    // 改变图片 保证图片不失真
    let moreImage = UIImage(named:images[2])?.withRenderingMode(.alwaysOriginal)
    let moreSelectImage = UIImage(named:selectedImages[2])?.withRenderingMode(.alwaysOriginal)
    
    MoreNC.tabBarItem = UITabBarItem.init(title: "更多", image: moreImage, selectedImage: moreSelectImage)
    
    let navArray = [HomeNC, CenterNC, MoreNC]
    let tabBarController = UITabBarController()
    tabBarController.viewControllers = navArray
    self.window?.rootViewController = tabBarController
    
    
    self.window!.makeKeyAndVisible()
    
    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:.
}

}

首页文件---HomeViewController.swift ---

代码语言:javascript
复制
import UIKit
class HomeViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.title = "首页"
    self.view.backgroundColor = UIColor.lightGray
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

个人中心页面 ---CenterViewController.swift ---

代码语言:javascript
复制
import UIKit 
class CenterViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.title = "个人中心"
    self.view.backgroundColor = UIColor.red
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

更多的页面---MoreViewController.swift ---

代码语言:javascript
复制
import UIKit 
class MoreViewController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    self.title = "更多"
    self.view.backgroundColor = UIColor.green
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

}

下载Demo:https://github.com/silencesmile/Swift_UITabBarController

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.11.08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档