gpt4 book ai didi

ios - iOS 9 中的 UISearchController - UISearchBar 中的 TextField 剪切出导航项

转载 作者:行者123 更新时间:2023-11-29 05:53:56 25 4
gpt4 key购买 nike

我试图在 iOS 9 上的导航项上获得平滑的搜索栏,这意味着我无法使用 navigationItem.searchController 属性,因为它仅适用于 iOS 11。

class SearchContainerViewController: UITableViewController {
let dataSource = ["1", "2", "3", "4", "5"]

override public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataSource.count
}

override public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: .default, reuseIdentifier: nil)
cell.textLabel?.text = dataSource[indexPath.row]

return cell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
dismiss(animated: true, completion: nil)
}
}

class SearchViewController: UISearchController {
override func viewDidLoad() {
super.viewDidLoad()
}
}

class MyViewController : UIViewController, UISearchResultsUpdating, UISearchBarDelegate {
lazy var searchButton = UIBarButtonItem(title: "Search", style: UIBarButtonItem.Style.plain, target: self, action: #selector(showSearchBar))

var searchViewController: SearchViewController = {
let container = SearchContainerViewController()
let searchController = SearchViewController(searchResultsController: container)

return searchController
}()

override func viewDidLoad() {
super.viewDidLoad()

setupSearchController()
setupSearchButton()
}

func setupSearchController() {
searchViewController.searchResultsUpdater = self
searchViewController.searchBar.delegate = self

searchViewController.dimsBackgroundDuringPresentation = false
searchViewController.hidesNavigationBarDuringPresentation = false
searchViewController.searchBar.searchBarStyle = .minimal
searchViewController.searchBar.showsCancelButton = true

definesPresentationContext = true
}

@objc func showSearchBar() {
UIView.animate(withDuration: 0.75) {
self.navigationItem.titleView = self.searchViewController.searchBar
self.navigationItem.rightBarButtonItem = nil
self.searchViewController.searchBar.becomeFirstResponder()
}
}

func setupSearchButton() {
UIView.animate(withDuration: 0.75) {
self.navigationItem.titleView = nil
self.navigationItem.rightBarButtonItem = self.searchButton
}
}

// MARK: Conforms to UISearchResultUpdating

public func updateSearchResults(for searchController: UISearchController) { }

func searchBarCancelButtonClicked(_ searchBar: UISearchBar) {
setupSearchButton()
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
view.layoutSubviews()
}
}


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
let newWindow = UIWindow(frame: UIScreen.main.bounds)

let mainViewController = MyViewController()
let navigationController = UINavigationController(rootViewController: mainViewController)

newWindow.backgroundColor = .white
newWindow.rootViewController = navigationController
newWindow.makeKeyAndVisible()

window = newWindow

return true
}
}

虽然结果有点令人失望,因为带有 StatusBar 的 TextView 从导航项上下文中剪切出来,但我做错了什么并且可以做得更好吗?

感谢您的支持。

最佳答案

在人们无缘无故地否决这个问题之前,我将做一些不同的事情并回答我自己的问题。

发生剪裁的原因是,在这两种情况下,navigationItem 的高度都不同,因为如果将大内容放入 titleView(如搜索栏)中,它们在某种程度上是可拉伸(stretch)的。

我从一开始就在 navigationItem 上设置了 searchBar,并在应该完成时更改了其 isHidden 属性。

    @objc private func activateSearch() {
UIView.animate(withDuration: 0.75) {
self.navigationItem.titleView?.isHidden = false
self.navigationItem.rightBarButtonItem = nil
self.searchController.isActive = true
}
}

private func deactivateSearch() {
UIView.animate(withDuration: 0.75) {
self.navigationItem.titleView?.isHidden = true
self.navigationItem.rightBarButtonItem = self.searchButton
self.searchController.isActive = false
}
}

关于ios - iOS 9 中的 UISearchController - UISearchBar 中的 TextField 剪切出导航项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55386598/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com