iOS11 设置导航栏、搜索框和TableView

两天前在WWDC上苹果发布了iOS11 beta1版本,让我们看下导航栏、搜索框和TableView有什么不一样的地方。

正常的导航栏

iOS11的导航栏

在viewDidLoad中加一段代码

override func viewDidLoad() {
  super.viewDidLoad()
  createData()
        
  // Navigation Bar
  navigationController?.navigationBar.prefersLargeTitles = true
}
复制代码

就可以得到这样的效果

向下滑动Title变成正常的状态,向上滑动Title变大 现在点击一个cell进入下一个ViewController

在NavigationController下的所有VC的Title都改变了 我们可以在一个VC中单独设置Title的样式 在需改修改Title样式的VC中设置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
}
复制代码
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值