1.新建模型

class Contact: NSObject {
var name : String?
var mobile : String?{
didSet{
if mobile?.lengthOfBytes(using: .utf8) == {
mobileString = mobile
var spaceIndex = mobile?.index(mobile!.startIndex, offsetBy: )
mobileString?.insert(" ", at: spaceIndex!)
spaceIndex = mobile?.index(mobile!.endIndex, offsetBy: -)
mobileString?.insert(" ", at: spaceIndex!)
}
}
}
var address : String?
var email : String?
var mobileString : String?
}

2.搜索控制器

class TestViewController: UIViewController,UISearchResultsUpdating{
func updateSearchResults(for searchController: UISearchController) { } var searchResultsCon = ResultsTableViewController() override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = #colorLiteral(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}
import UIKit

class ResultsTableViewController: UITableViewController {
var contacts = Array<Contact>()
override func viewDidLoad() {
super.viewDidLoad() // Uncomment the following line to preserve selection between presentations
// self.clearsSelectionOnViewWillAppear = false // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem
} override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
} // MARK: - Table view data source override func numberOfSections(in tableView: UITableView) -> Int {
// #warning Incomplete implementation, return the number of sections
return
} override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows
return contacts.count
} override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "reuseID")
if contacts.count > indexPath.row {
let contact = contacts[indexPath.row]
cell.textLabel!.text = contact.mobileString
}
cell.textLabel!.textColor = UIColor.brown
return cell
}

3.主控制器

class ViewController: UITableViewController,UISearchResultsUpdating{
var searchResultsCon = ResultsTableViewController()
var contacts = Array<Contact>()
override func viewDidLoad() {
super.viewDidLoad()
configureTableView()
configureNavigationBar()
}
func configureTableView(){
tableView.estimatedRowHeight = 44.0
tableView.estimatedSectionHeaderHeight = 0.0
tableView.estimatedSectionFooterHeight = 0.0
for _ in ... {
let temp = Int(arc4random_uniform()) +
let contact = Contact()
contact.mobile = String(temp)
contacts.append(contact)
}
}
func configureNavigationBar(){
if #available(iOS 11.0, *) {
self.navigationController?.navigationBar.prefersLargeTitles = true
//导航栏文本颜色
self.navigationController?.navigationBar.titleTextAttributes = [.foregroundColor:#colorLiteral(red: 0.9254902005, green: 0.2352941185, blue: 0.1019607857, alpha: )]
self.navigationController?.navigationBar.largeTitleTextAttributes = [.foregroundColor:#colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: )]
//search
let mySearchController: UISearchController = UISearchController(searchResultsController: searchResultsCon)
mySearchController.searchResultsUpdater = self
// 设置模态出结果页搜索前不隐藏导航栏
// mySearchController.hidesNavigationBarDuringPresentation = false
self.navigationItem.searchController = mySearchController
// 搜索框默认显示并且滚动不消失
self.navigationItem.hidesSearchBarWhenScrolling = false
//当模态出结果页搜索后不隐藏导航栏
self.definesPresentationContext = true
//刷新控件
tableView.refreshControl = UIRefreshControl()
tableView.refreshControl?.addTarget(self, action: #selector(self.refreshData), for: .valueChanged)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
//MARK -- tableView
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return contacts.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "reuseID")
if contacts.count > indexPath.row {
let contact = contacts[indexPath.row]
cell.textLabel?.text = contact.mobileString
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
tableView.deselectRow(at: indexPath, animated: true)
self.navigationController?.pushViewController(TestViewController(), animated: true)
}
@objc func refreshData(){
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + .seconds(), execute: {
self.tableView.refreshControl?.endRefreshing()
})
}
//MARK -- searchController
@available(iOS 8.0, *)
func updateSearchResults(for searchController: UISearchController) {
let searchText = searchController.searchBar.text!
let contactList = contacts.filter({ (con) -> Bool in
return (con.mobile?.hasPrefix(searchText))!
})
searchResultsCon.contacts = contactList
searchResultsCon.tableView.reloadData() }
override func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
// 搜索框滚动时消失
self.navigationItem.hidesSearchBarWhenScrolling = true
}
}

IOS11 - UINavigationItem大标题,搜索栏实现的更多相关文章

  1. iOS11 仿大标题 导航栏

    iOS11 SytleTitleController  仿大标题 风格 导航栏 仿 iOS11 大导航标题 风格 UI 适用范围 iOS8 + 前言 iOS11全面应用大标题设计,(岂止于大—— 比逼 ...

  2. word2007二级标题自动编号不从大标题开始的解决方法

    今天在编写word文档的时候,遇到一个很奇怪的问题,word2007二级标题自动编号不从大标题开始,可能我说的比较模糊,我截个图大家一看就明白了. 我想要的是2.1 2.2结果,他确是从1.1开始了. ...

  3. ggplot2画histogram(坐标轴刻度值字体大小,坐标轴标题字体大小,柱形宽度,大标题字体大小、居中)

    见链接:https://www.r-bloggers.com/how-to-make-a-histogram-with-ggplot2/ 写的很完整. 此外,关于一些参数的用法: theme(plot ...

  4. iOS11、iPhone X、Xcode9 适配

    更新iOS11后,发现有些地方需要做适配,整理后按照优先级分为以下三类: 1.单纯升级iOS11后造成的变化: 2.Xcode9 打包后造成的变化: 3.iPhoneX的适配 一.单纯升级iOS11后 ...

  5. iOS之iOS11、iPhone X、Xcode9 适配指南

    更新iOS11后,发现有些地方需要做适配,整理后按照优先级分为以下三类: 1.单纯升级iOS11后造成的变化: 2.Xcode9 打包后造成的变化: 3.iPhoneX的适配 一.单纯升级iOS11后 ...

  6. iOS11、iPhone X、Xcode9 适配指南

    更新iOS11后,发现有些地方需要做适配,整理后按照优先级分为以下三类: 1.单纯升级iOS11后造成的变化: 2.Xcode9 打包后造成的变化: 3.iPhoneX的适配 一.单纯升级iOS11后 ...

  7. 你可能需要为你的APP适配iOS11

    WeTest 导读  iOS 11 为整个生态系统的 UI 元素带来了一种更加大胆.动态的新风格. 本文介绍了iOS11在UI方面做了哪些更新,有些更新可以为用户提供更加完美的体验,但也有的可能会给目 ...

  8. iOS -- iOS11新特性,如何适配iOS11

    前言 这几天抽空把WWDC的Session看了一些,总结了一些iOS11新的特性,可能对我们的App有影响,需要我们进行适配.本文作为一个总结. 本文内容包括:集成了搜索的大标题栏.横向选项卡栏.Ma ...

  9. HTML5学习笔记三 HTML元素、属性、标题、段落简介

    一.HTML 元素 HTML 元素以开始标签起始 HTML 元素以结束标签终止 元素的内容是开始标签与结束标签之间的内容 某些 HTML 元素具有空内容(empty content) 空元素在开始标签 ...

随机推荐

  1. vs2005 未能完成操作。未指定的错误

    具体解决过程是这样的: 1.先把.vcproj 文件剪切到其他地方 2.打开.sln,报错->点“确定”->再点“确定” 3.把 .vcproj 文件 放回来,在vs2008右边的“解决方 ...

  2. 用live555做流媒体转发服务器?

    当我们看到这里,说明大家都有这样的一个想法:那就是如何用live555实现一个直播代理转发的流媒体服务器? 我们先不着急去讨论用live555实现流媒体转发的技术方法123,先从live555的整个架 ...

  3. Rethinking the Inception Architecture for Computer Vision

    https://arxiv.org/abs/1512.00567 Convolutional networks are at the core of most state-of-the-art com ...

  4. Hadoop实战-Flume之Hdfs Sink(十)

    a1.sources = r1 a1.sinks = k1 a1.channels = c1 # Describe/configure the source a1.sources.r1.type = ...

  5. 在Win7创建WiFi热点

    1.在开始菜单搜索cmd 2.运行cmd 3.输入以下命令,注意:Your-WiFi-Name和Your-WiFi-Password分别为WiFi名称和密码 netsh wlan set hosted ...

  6. 【LeetCode】Maximum Depth of Binary Tree

    http://oj.leetcode.com/problems/maximum-depth-of-binary-tree/ public class Solution { public int max ...

  7. 关于Fragment的onActivityResult 不执行

    1.getActivity().startActivityForResult();  与 fragment.startActivityForActivity(): getActivity().star ...

  8. 函数----Function对象

    函数---Function对象 一 .  函数的书写 函数 : 就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 函数的作用 : ● 将大量重复的语句写在函数里,以后需要这些语句的时候,可以 ...

  9. jquery特效(6)—判断复选框是否选中进行答题提示

    前面有一段时间思想开了小差,跟着师父学习了一段时间才发现差距很大,看来我要奋起直追~\(≧▽≦)/~啦啦啦. 最近公司在做一个项目,需要根据用户选择的选项给出相应的提示,下面来看我写的测试程序的效果: ...

  10. valid No such filter: 'drawtext'"

    libfreetype is missing. You'll have to rebuild FFmpeg with this library or disable overlays. --enabl ...