
上一篇 開始用Swift開發(fā)iOS 10 - 19 使用UIPageViewController構(gòu)建介紹頁面構(gòu)建了開始介紹頁面,這一篇學(xué)習(xí)使用Tab Bar Controller和拆分Storyboard。
創(chuàng)建 Tab Bar Controller
- 選擇開始的Navigation Controller,Editor > Embed in > Tab Bar Controller,然后自動(dòng)添加一個(gè)新的Tab Bar Controller 作為初始的控制器,之前Navigation Controller變成Tab Bar Controller的一部分了。

- 在Navigation Controller中選擇 tab item,修改
System Item為Favorites。
隱藏 Tab Bar
工具欄在其他頁面隱藏,選擇Detail View,勾選Hide Bottom Bar on Push就可以。

也可以用代碼形式,在RestaurantTableViewController中的prepare(for:)中添加:
destinationController.hidesBottomBarWhenPushed = true
添加新的Tabs
- 添加一個(gè)新的Navigation Controller, 選中tab item,修改
System Item為Recents;table view的navigation item的title為Discover

-
把新加的Navigation Controller加入到 Tab Bar Controller。用ctrl+drag從Tab Bar Controller到新的Navigation Controller,選擇
Relationship Segue - View Controllers。
改變新的Navigation Controller的tab bar的title為
Recent,修改system item為Recents同樣的方式添加一個(gè)新的Navigation Controller, 選中tab item,修改
System Item為More;table view的navigation item的title為About。也用ctrl+drag與Tab Bar Controller建立關(guān)系。
最終創(chuàng)建有三個(gè)Tab Bar Item的Tab Bar。

定制Tab Bar的樣式
修改Tab Bar的樣式在application(_:didFinishLaunchingWithOptions:)中進(jìn)行,類似下面的代碼,詳細(xì)的API可參考官方文檔:
UITabBar.appearance().tintColor = UIColor(red: 235.0/255.0, green: 75.0/255.0,
blue: 27.0/255.0, alpha: 1.0)
UITabBar.appearance().barTintColor = UIColor.black
改變 Tab Bar Item的圖片
- 從圖片下載圖片到
Assets.xcasset。 - 在SB中修改三個(gè)Tab Bar Item的System Item都為
Custom,修改Title和Image為想要的值,類似下圖

- 在
application(_:didFinishLaunchingWithOptions:)中修改樣式:
UITabBar.appearance().tintColor = UIColor(red: 235.0/255.0, green: 75.0/255.0, blue: 27.0/255.0, alpha: 1.0)
UITabBar.appearance().barTintColor = UIColor(red: 236/255.0, green: 240.0/255.0, blue: 241.0/255.0, alpha: 1.0)

改變選擇指示圖片(Selection Indicator Image)
Selection Indicator Image就是指tab bar item被選中后與其它tab bar item不同的樣式,在代碼中和SB中都可以修改。
UITabBar.appearance().selectionIndicatorImage = UIImage(named: "tabitem-selected")

最終效果:

拆分Storyboard
當(dāng)項(xiàng)目變大時(shí),一個(gè)Storyboard就會(huì)很大,如果是項(xiàng)目中有很多人合作的話,一個(gè)Storyboard也很難管理。從Xcode7之后,新功能storyboard references解決了這個(gè)問題。
- 在
Main.storyboard中選擇,about相關(guān)的兩個(gè)view controller,然后Editor > Refactor to Storyboard...,新建名為about.storyboard的storyboard文件,就把這個(gè)兩個(gè)獨(dú)立出去了。

把a(bǔ)bout相關(guān)的view controller拆分出去后,在Main.storyboard里有一個(gè)關(guān)聯(lián)圖標(biāo),雙擊就可以跳轉(zhuǎn)到about.storyboard中。

- 同樣的方法,獨(dú)立出discover相關(guān)view controller,生成
discover.storyboard文件。
代碼
Beginning-iOS-Programming-with-Swift
說明
此文是學(xué)習(xí)appcode網(wǎng)站出的一本書 《Beginning iOS 10 Programming with Swift》 的一篇記錄
系列文章目錄
- 開始用Swift開發(fā)iOS 10 - 1 前言
- 開始用Swift開發(fā)iOS 10 - 2 Hello World!第一個(gè)Swift APP
- 開始用Swift開發(fā)iOS 10 - 3 介紹Auto Layout
- 開始用Swift開發(fā)iOS 10 - 4 用Stack View設(shè)計(jì)UI
- [開始用Swift開發(fā)iOS 10 - 5 原型的介紹]
- 開始用Swift開發(fā)iOS 10 - 6 創(chuàng)建簡(jiǎn)單的Table Based App
- 開始用Swift開發(fā)iOS 10 - 7 定制Table Views
- 開始用Swift開發(fā)iOS 10 - 8 Table View和UIAlertController的交互
- 開始用Swift開發(fā)iOS 10 - 9 Table Row的刪除, UITableViewRowAction和UIActivityViewController的使用
- 開始用Swift開發(fā)iOS 10 - 10 Navigation Controller的介紹和Segue
- 開始用Swift開發(fā)iOS 10 - 11 面向?qū)ο缶幊探榻B
- 開始用Swift開發(fā)iOS 10 - 12 豐富Detail View和定制化Navigation Bar
- 開始用Swift開發(fā)iOS 10 - 13 Self Sizing Cells and Dynamic Type
- 開始用Swift開發(fā)iOS 10 - 14 基礎(chǔ)動(dòng)畫,模糊效果和Unwind Segue
- 開始用Swift開發(fā)iOS 10 - 15 使用地圖
- 開始用Swift開發(fā)iOS 10 - 16 介紹靜態(tài)Table Views,UIImagePickerController和NSLayoutConstraint
- 開始用Swift開發(fā)iOS 10 - 17 使用Core Data
