【问题标题】:UISearchBar - Disable animation when searchController.hidesNavigationBarDuringPresentation is falseUISearchBar - 当 searchController.hidesNavigationBarDuringPresentation 为 false 时禁用动画
【发布时间】:2022-01-20 04:35:36
【问题描述】:

searchController.hidesNavigationBarDuringPresentation设置为false时如何禁用UISearchBarController动画?

    private func setupSearchBar() {
        searchController.hidesNavigationBarDuringPresentation = false
        customizeSearchBar()

        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
    }

    private func customizeSearchBar() {
        let searchBar = searchController.searchBar
        searchBar.searchBarStyle = .minimal
        searchBar.setPositionAdjustment(UIOffset(horizontal: 15.0, vertical: 0.0), for: .search)
        searchBar.setPositionAdjustment(UIOffset(horizontal: -10.0, vertical: 0.0), for: .clear) 
        searchBar.setShowsCancelButton(false, animated: false)
        
        let searchTextField = searchBar.searchTextField
        searchTextField.backgroundColor = .red
        searchTextField.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            searchTextField.heightAnchor.constraint(equalToConstant: 46),
            searchTextField.leadingAnchor.constraint(equalTo: searchBar.leadingAnchor, constant: 10),
            searchTextField.trailingAnchor.constraint(equalTo: searchBar.trailingAnchor, constant: -10),
            searchTextField.centerYAnchor.constraint(equalTo: searchBar.centerYAnchor, constant: 0)
        ])
        searchTextField.attributedPlaceholder = NSAttributedString(
            string: NSLocalizedString("Search for...", comment: ""),
            attributes: [ .foregroundColor: UIColor.custom(color: .cSlateGrey) ]
        )
        searchTextField.leftView?.tintColor = .custom(color: .cSlateGrey)
        searchTextField.bounds.inset(by: UIEdgeInsets(top: 0, left: 30, bottom: 0, right: 30))
        searchTextField.backgroundColor = .custom(color: .cWhite)
        
        searchTextField.roundCorners(radius: 23)
    }

每次点击搜索栏时,它都会“跳跃”。尝试搜索但找不到任何东西。

编辑:导航栏下方有一个collectionView,看起来是因为collection view而跳动,一旦我删除它,搜索栏就会按预期工作。

【问题讨论】:

  • 将搜索栏单独设置在外面而不是在导航中,那么它会在没有跳转的情况下正常工作。

标签: ios swift uikit uisearchbar


【解决方案1】:

此问题是由导航栏下方的空集合视图引起的。 通过将集合视图限制在屏幕顶部而不是安全区域来修复它。

【讨论】:

    猜你喜欢
    • 2020-02-22
    • 2016-09-21
    • 1970-01-01
    • 2013-10-28
    • 2012-02-28
    • 2017-02-18
    • 1970-01-01
    • 2012-04-19
    • 2013-02-08
    相关资源
    最近更新 更多