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. android handler looper

    http://www.cnblogs.com/plokmju/p/android_Handler.html

  2. 分布式流媒体直播服务器系统 For Linux

    在之前的一篇<基于Darwin实现的分布式流媒体直播服务器系统>中,我们配置了在Win32下面的流媒体直播系统,今天我们分享一下在Linux下面EasyDSS分布式直播服务器系统的配置. ...

  3. EasyDarwin开源平台直播架构

    Created with Raphaël 2.1.0ClientClientEasyCMSEasyCMSEasyCameraEasyCameraEasyDarwinEasyDarwin请求设备列表设备 ...

  4. Vue 向下扩展后就类似于 jQuery

    https://cn.vuejs.org/v2/guide/comparison.html

  5. sam模板

    SAM模板 struct SAM{ * ; struct node{ node*nxt[],*fail; int len; }; node*root;int cnt; node no[maxn]; n ...

  6. codeforces 465C.No to Palindromes! 解题报告

    题目链接:http://codeforces.com/problemset/problem/464/A 题目意思:给出一个长度为 n 且是 tolerable 的字符串s,需要求出字典序最小的长度也为 ...

  7. mongodb存储二进制数据的二种方式——binary bson或gridfs

    python 版本为2.7 mongodb版本2.6.5 使用mongodb存储文件,可以使用两种方式,一种是像存储普通数据那样,将文件转化为二进制数据存入mongodb,另一种使用gridfs,咱们 ...

  8. 【概念】SVG(2)

    Style <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg&qu ...

  9. dancing link 精确覆盖 重复覆盖 (DLX)

    申明:因为转载的没有给出转载链接,我就把他的链接附上,请尊重原创: http://www.cnblogs.com/-sunshine/p/3358922.html 如果谁知道原创链接 给一下,请尊重原 ...

  10. 【C/C++】计算两个整数的最大公约数和最小公倍数

    算法一 任何>1的整数都可以写成一个或多个素数因子乘积的形式,且素数乘积因子以非递减序出现. 则整数x,y可以分别标记为:x=p1x1p2x2...pmxm y=p1y1p2y2...pmym ...