IOS11 - UINavigationItem大标题,搜索栏实现
2024-09-07 23:20:28
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
}
}
最新文章
- 绘制扇形效果线条小Bug解决
- C和指针 第十五章 错误报告perror和exit
- 在WPF中使用变通方法实现枚举类型的XAML绑定
- 收集的在线图片压缩(jpg/png)
- css 图片内容在不同分辨率下居中显示(演示的图片宽度是1920px,当图片宽度大于显示屏的宽度时)
- springmvc No mapping found for HTTP request with URI in Dispatc
- TCP 连接的 TIME_WAIT 过多 导致 Tomcat 假死
- 万网云解析设置二级域名解析到同IP不同端口
- Python中的__new__()方法的使用
- TS流格式(转)
- DB天气安卓客户端测试计划
- IE6下整站的bug详解
- 盼盼Degenerate——清除浮动的方法
- 自己动手修改Robotium代码(上)
- ASP.NET部分代码示例
- UNIX网络编程——心跳包
- Runnable与Callable区别
- mysql问题汇总——持续更新
- 程序编译运行和exe运行之文件位置的区别
- pygame(class类)调用视图的方法