两天前在WWDC上苹果发布了iOS11 beta1版本,让我们看下导航栏、搜索框和TableView有什么不一样的地方。
正常的导航栏
iOS11的导航栏
在 viewDidLoad
中加一段代码
override func viewDidLoad() {
super.viewDidLoad()
createData()
// Navigation Bar
navigationController?.navigationBar.prefersLargeTitles = true
}
就可以得到这样的效果
向下滑动 Title 变成正常的状态,向上滑动Title变大
现在点击一个 cell 进入下一个控制器
在 NavigationController 下的所有控制器的 Title 都改变了
我们可以在一个控制器中单独设置 Title 的样式
在需改修改 Title 样式的控制器中设置 navigationItem.largeTitleDisplayMode
属性来改变 Title 的类型
override func viewDidLoad() {
super.viewDidLoad()
// 自动设置
// navigationController?.navigationItem.largeTitleDisplayMode = .automatic
// 大标题
// navigationController?.navigationItem.largeTitleDisplayMode = .always
// 小标题
navigationController?.navigationItem.largeTitleDisplayMode = .never
}
现在 Title 变成原始状态了
Tip:上述的代码可以在 Xcode9 中的 storyboard 上直接设置
iOS11的搜索框
添加一个搜索框
override func viewDidLoad() {
super.viewDidLoad()
createData()
// Navigation Bar
navigationController?.navigationBar.prefersLargeTitles = true
// Search Controller
let mySearchController: UISearchController = UISearchController(searchResultsController: UIViewController())
navigationItem.searchController = mySearchController
}
效果图
可以看到搜索框会下拉出现,上拉隐藏,这些系统都帮我们做好了,我们只需要设置一下就可以有这样的效果
iOS11的 UITableView
UITableView
多了两个代理方法
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration?
先看下效果
我们可以看到 iOS11 中多了从左侧划出来的 action
大幅度向一个方向滑动会直接触发 action
具体的实现方法
/// 从左侧划出
override func tableView(_ tableView: UITableView, leadingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .normal, title: "Mark") { (action, view, handler) in
self.updateMarkState(for: indexPath)
handler(true)
}
action.backgroundColor = UIColor(colorLiteralRed: 73/255.0, green: 175/255.0, blue: 254/255.0, alpha: 1)
if markState(for: indexPath) {
action.title = "Unmark"
action.backgroundColor = UIColor.red
}
let configuration = UISwipeActionsConfiguration(actions: [action])
return configuration
}
/// 从右侧划出
override func tableView(_ tableView: UITableView, trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath) -> UISwipeActionsConfiguration? {
let action = UIContextualAction(style: .destructive, title: "Delete") { (action, view, handler) in
self.removeItem(at: indexPath)
handler(true)
}
let configuration = UISwipeActionsConfiguration(actions: [action])
return configuration
}
可以设置多个action
let configuration = UISwipeActionsConfiguration(actions: [action, ...])
上述的3个特性仅在iOS11上有效
如果要兼容低版本需要判断当前系统的版本
if #available(iOS 11.0, *) {
navigationController?.navigationBar.prefersLargeTitles = true
} else {
// Fallback on earlier versions
}