上一篇 開始用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,然后自動添加一個新的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
- 添加一個新的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
同樣的方式添加一個新的Navigation Controller, 選中tab item,修改
System Item
為More
;table view的navigation item的title為About
。也用ctrl+drag與Tab Bar Controller建立關(guān)系。
最終創(chuàng)建有三個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中修改三個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)項目變大時,一個Storyboard就會很大,如果是項目中有很多人合作的話,一個Storyboard也很難管理。從Xcode7之后,新功能storyboard references
解決了這個問題。
- 在
Main.storyboard
中選擇,about相關(guān)的兩個view controller,然后Editor > Refactor to Storyboard...,新建名為about.storyboard
的storyboard文件,就把這個兩個獨立出去了。
把a(bǔ)bout相關(guān)的view controller拆分出去后,在Main.storyboard
里有一個關(guān)聯(lián)圖標(biāo),雙擊就可以跳轉(zhuǎn)到about.storyboard
中。
- 同樣的方法,獨立出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!第一個Swift APP
- 開始用Swift開發(fā)iOS 10 - 3 介紹Auto Layout
- 開始用Swift開發(fā)iOS 10 - 4 用Stack View設(shè)計UI
- [開始用Swift開發(fā)iOS 10 - 5 原型的介紹]
- 開始用Swift開發(fā)iOS 10 - 6 創(chuàng)建簡單的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ǔ)動畫,模糊效果和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