iOS26适配指南之UISearchController

介绍

在 iOS 26 中,Apple 对 UISearchController 做出了两项重要改进:

  1. 搜索栏在 UINavigationItem 中的布局更加灵活。
  2. 可以直接将搜索栏集成到 UIToolbar 中。

搜索栏在导航栏中的新布局

iOS 26 中如果 UISearchController 集成在 UINavigationItem,默认情况下搜索栏会显示在底部,如果希望像之前在顶部显示,可以将 UINavigationItem 的preferredSearchBarPlacement属性设置为UINavigationItem.SearchBarPlacement.stacked

案例

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "abc")
        tableView.dataSource = self
        tableView.rowHeight = 60.0
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()

    lazy var searchController: UISearchController = {
        let controller = UISearchController(searchResultsController: nil)
        controller.searchBar.searchBarStyle = .minimal
        controller.searchBar.placeholder = "搜索学校"
        return controller
    }()

    let schoolArray = ["清华大学", "北京大学", "中国人民大学", "北京交通大学", "北京工业大学",
                       "北京航空航天大学", "北京理工大学", "北京科技大学", "中国政法大学",
                       "中央财经大学", "华北电力大学", "北京体育大学", "上海外国语大学", "复旦大学",
                       "华东师范大学", "上海交通大学", "同济大学", "上海财经大学", "华东理工大学"]

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.largeTitle = "大学列表"
        navigationItem.searchController = searchController
        // 恢复成之前的顶部显示
        navigationItem.preferredSearchBarPlacement = .stacked
    }
}

// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return schoolArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "abc", for: indexPath)
        cell.textLabel?.text = schoolArray[indexPath.row]
        return cell
    }
}

效果

UISearchController支持UIToolbar集成

iOS 26 之前 UISearchController 只能出现在导航栏或者内容视图顶部,而现在可以将搜索栏直接放入 UIToolbar,打造一种更轻盈、紧凑的搜索体验。

案例

代码

swift 复制代码
import UIKit

class ViewController: UIViewController {
    lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.register(UITableViewCell.self, forCellReuseIdentifier: "abc")
        tableView.dataSource = self
        tableView.rowHeight = 60.0
        tableView.translatesAutoresizingMaskIntoConstraints = false
        return tableView
    }()

    lazy var searchController: UISearchController = {
        let controller = UISearchController(searchResultsController: nil)
        controller.searchBar.searchBarStyle = .minimal
        controller.searchBar.placeholder = "搜索学校"
        return controller
    }()

    let schoolArray = ["清华大学", "北京大学", "中国人民大学", "北京交通大学", "北京工业大学",
                       "北京航空航天大学", "北京理工大学", "北京科技大学", "中国政法大学",
                       "中央财经大学", "华北电力大学", "北京体育大学", "上海外国语大学", "复旦大学",
                       "华东师范大学", "上海交通大学", "同济大学", "上海财经大学", "华东理工大学"]

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(tableView)
        NSLayoutConstraint.activate([
            tableView.topAnchor.constraint(equalTo: view.topAnchor),
            tableView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            tableView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            tableView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ])

        navigationController?.navigationBar.prefersLargeTitles = true
        navigationItem.largeTitle = "大学列表"
        navigationItem.searchController = searchController
        navigationController?.setToolbarHidden(false, animated: false)
        // iOS26新增,允许将searchBar集成到UIToolbar
        navigationItem.searchBarPlacementAllowsToolbarIntegration = true
        let flexibleSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let addBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: nil)
        let refreshBarButtonItem = UIBarButtonItem(barButtonSystemItem: .refresh, target: self, action: nil)
        // 将searchBar集成到UIToolbar
        toolbarItems = [addBarButtonItem, navigationItem.searchBarPlacementBarButtonItem, flexibleSpace, refreshBarButtonItem]
    }
}

// MARK: - UITableViewDataSource
extension ViewController: UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return schoolArray.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "abc", for: indexPath)
        cell.textLabel?.text = schoolArray[indexPath.row]
        return cell
    }
}

效果

相关推荐
ZZH_AI项目交付2 小时前
同一套前摄心率算法,为什么 iPhone 16 Pro 稳,iPhone XR 会失准?
ios·app·ai编程
DeMinds2 小时前
内容没有丢,我为什么总在重新整理?|DeMinds 如何让工作接着继续
ios·github·markdown
apd_csdn3 小时前
清华邮箱苹果邮件app设置(全)
ios·thu
MDM.Plus4 小时前
从“遥控”到“自治”:苹果 MDM 技术的代际跨越与业务重构
ios·智能手机·重构·mdm
黑化旺仔4 小时前
iOS - 天气预报仿写总结
ios
Coffeeee5 小时前
ios零基础的Android开发能否靠AI让老板省一笔人工费呢
android·ios·ai编程
2501_916007475 小时前
深入理解HTTPS对称与非对称加密机制及Charles抓包实践
网络协议·http·ios·小程序·https·uni-app·iphone
2501_916008895 小时前
HTTPS 抓包遇到证书绑定怎么办,使用 TraceEagle 解除 App 证书校验
网络协议·计算机网络·http·网络安全·ios·adb·https
白玉cfc6 小时前
【iOS】MRC和ARC
macos·ios·cocoa
2501_915921437 小时前
从零开始学 Swift iOS 开发 iOS应用入门
ide·vscode·ios·objective-c·个人开发·swift·敏捷流程