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

屏幕快照 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

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 ---

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 ---

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 ---

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

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏刘晓杰

RN实现ListView

4059
来自专栏hrscy

iOS百度地图开发之路径规划

3903
来自专栏iOS Developer

Swift纯代码走进UICollectionView

5922
来自专栏wOw的Android小站

[iOS] 小问题记录

iOS 往数据库里写保存文件路径的时候,不要写全路径,因为软件更新或者重新安装沙盒路径会变

1102
来自专栏Rindew的iOS技术分享

解决iOS 10 之后调用拨号,系统弹窗延迟

2686
来自专栏coding...

iOS开发-RAC+MVVM练手项目 图床App写在前面准备工作界面设计首页历史关于后记

前段时间闲着无聊和盆友就搞了个图床站Chevereto-Free,忽然发现居然有API提供,而且很简单,只需要一个KEY就可以

1162
来自专栏Alice

demo1 动态显示view或弹框 动态隐藏view或弹框

实现界面如上所示: 有一个弹框,弹框上边有一个关闭按钮,点击按钮,可以关闭弹框。点击弹框的周围区域也可以关闭按钮。 点击上边的隐藏弹框也可以关闭按钮。 在实现...

2115
来自专栏進无尽的文章

UI篇-UITabBar及其相关其他知识

和UINavigationController类似,UITabBarController也可以轻松地管理多个控制器,轻松完成控制器之间的切换。

3112
来自专栏Alice

demo1 动态显示view或弹框 动态隐藏view或弹框

有一个弹框,弹框上边有一个关闭按钮,点击按钮,可以关闭弹框。点击弹框的周围区域也可以关闭按钮。 点击上边的隐藏弹框也可以关闭按钮。   在实现功能的基础上,以...

2227
来自专栏谈补锅

Quartz2D复习(三) --- 涂鸦

和上一篇手势解锁不一样,手势解锁只画了一条路径,从触摸开始--》触摸移动--》触摸结束 ,然后路径完成了,渲染出来就是手势解锁了;

1123

扫码关注云+社区

领取腾讯云代金券