iOS 11 SDK 介紹

navigation bar large title,放大的 navigation bar

範例: Setting App

controller 個別控制 navigation bar 是否放大

捲動後在 navigation bar 裡顯示 search bar

捲動後 navigation bar 縮小

實作:

讓 navigation bar 放大。

將 navigation bar 的 prefersLargeTitles 設成 true。

open var prefersLargeTitles: Bool

每個頁面的 navigation item 可控制 navigation bar 是否放大,透過 largeTitleDisplayMode。

open var largeTitleDisplayMode: UINavigationItem.LargeTitleDisplayMod

automatic : 以前一頁 navigation item 的 largeTitleDisplayMode 為準。

always: 放大。

never: 不放大。

public enum LargeTitleDisplayMode : Int {
case automatic
case always
case never
}

捲動後 navigation bar 縮小

當畫面上的 Scroll View, Table View, Collection View, Text View 捲動時,都可以讓 navigation bar 縮小。

捲動後在 navigation bar 裡顯示 search bar

透過 navigation item 的 2 個屬性。

open var searchController: UISearchController?
open var hidesSearchBarWhenScrolling: Bool

範例:

class MovieTableViewController: UITableViewController, UISearchResultsUpdating {
var movies = [“your name”, “my name”, “her name”]
var searchMovies = [String]()
@available(iOS 8.0, *)
func updateSearchResults(for searchController: UISearchController) {
let searchString = searchController.searchBar.text!
searchMovies = movies.filter { (name) -> Bool in
return name.contains(searchString)
}
tableView.reloadData()
}
override func viewDidLoad() {
super.viewDidLoad()
let searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.dimsBackgroundDuringPresentation = false
navigationItem.searchController = searchController
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if navigationItem.searchController!.isActive {
return searchMovies.count
}
else {
return movies.count
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: “MovieCell”, for: indexPath)
// Configure the cell…
if navigationItem.searchController!.isActive {
cell.textLabel?.text = searchMovies[indexPath.row]
}
else {
cell.textLabel?.text = movies[indexPath.row]
}
return cell
}

讓 search bar 固定顯示在畫面上

navigationItem.hidesSearchBarWhenScrolling = false

navigation bar 上的下拉更新

Safe Area

View 的 safe area 控制內容呈現的範圍。

class UIView {
var safeAreaLayoutGuide: UILayoutGuide { get }
var safeAreaInsets: UIEdgeInsets { get }
func safeAreaInsetsDidChange()
}

勾選 Use Safe Area Layout Guides 後,Top Layout Guide 和 Bottom Layout Guide 被拿掉,變成使用 Safe Area。

範例 1:

圖片的上下左右間距,分別對應到 Safe Area 的 trailing,leading,bottom 和 top。

範例 2:

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
print(view.safeAreaInsets)
print(view.safeAreaLayoutGuide)
}

標準大小的 navigaiton bar 時,safe area 的 top inset 是 64

UIEdgeInsets(top: 64.0, left: 0.0, bottom: 0.0, right: 0.0)

<UILayoutGuide: 0x6100001bce00 — “UIViewSafeAreaLayoutGuide”, layoutFrame = {{0, 64}, {375, 603}}, owningView = <UIView: 0x7fb3095185c0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x60800003d140>>>

放大版的 navigaiton bar 時,safe area 的 top inset 是 116。

UIEdgeInsets(top: 116.0, left: 0.0, bottom: 0.0, right: 0.0)

<UILayoutGuide: 0x6100001ab1a0 — “UIViewSafeAreaLayoutGuide”, layoutFrame = {{0, 116}, {375, 551}}, owningView = <UIView: 0x7ff4d422efe0; frame = (0 0; 375 667); autoresize = W+H; layer = <CALayer: 0x618000033460>>>

不再失真的 pdf 向量圖片

客製表格(table) 的 swipe action

可客製左滑/右滑 cell 時顯示多個點選的 button ,透過定義 UITableViewDelegate 的以下 function 實現。

@available(iOS 11.0, *)
optional public func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
@available(iOS 11.0, *)
optional public func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?

預設的左滑 delete

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
return nil
}

客製左滑顯示的 button。

override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let likeAction = UIContextualAction(style: .normal, title: “一點點動心”) { (action, view, completionHandler) in
print(“一點點動心”)
completionHandler(true)
}
let deeploveAction = UIContextualAction(style: .normal, title: “深愛”) { (action, view, completionHandler) in
print(“深愛”)
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [likeAction, deeploveAction])
}

注明: 記得要呼叫 completionHandler,否則點選 button 將,cell 不會回到正常顯示的狀態。

客製滑動後,顯示的 button 的背景顏色和圖片。

deeploveAction.backgroundColor = UIColor.orange
deeploveAction.image = UIImage(named: “Download”)

防止滑到底觸發第一個 button 的 action

將 performsFirstActionWithFullSwipe 設為 false。

let configuration = UISwipeActionsConfiguration(actions: [likeAction, deeploveAction])
configuration.performsFirstActionWithFullSwipe = false

客製右滑顯示的 button。

override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let notLikeAction = UIContextualAction(style: .normal, title: “一點點不動心”) { (action, view, completionHandler) in
print(“一點點不動心”)
completionHandler(false)
}
let noFeelingAction = UIContextualAction(style: .normal, title: “完全沒感覺”) { (action, view, completionHandler) in
print(“完全沒感覺”)
completionHandler(true)
}
return UISwipeActionsConfiguration(actions: [notLikeAction, noFeelingAction])
}

ARKit

可參考 Apple 官方範例

https://developer.apple.com/sample-code/wwdc/2017/PlacingObjects.zip

Core ML & Vision

Vision: 人臉辨識,文字辨識,圖片辨識,barcode 辨識,影片辨識

Vision 範例:

Drag & Drop

App Demo:

Safari App: 移動 search bar 裡的文字

Notes App: 移動照片

移動文字,圖片,檔案

可在 App 裡 Drog & Drop,也可在不同 App 間 Drag & Drop (不同 App 間只有 iPad 支援)

可以多選後再 Drag & Drop

MusicKit

存取 Apple Music 的音樂

Business Chat

透過 Messages app 聊天

SKStoreReviewController

Apple announced that starting with iOS 11, developers will no longer be able to ask users for ratings in their apps unless they are using the built-in ratings controller. The controller is a part of the iOS SDK called the SKStoreReviewController, and it’s available in the StoreKit library starting in iOS 10.3 SDK.

import StoreKit
SKStoreReviewController.requestReview()

上架的 App,不一定會顯示

開發測試時,一定會顯示

File Management

讀取本機和網路上的檔案,可搭配 UIDocumentBrowserViewController。

Core NFC