搜索栏取消按钮仅在 iPad 上不可见

问题描述 投票:0回答:4

首先我想说我没有在

Swift
中找到任何好的答案。我创造了
search bar
。单击按钮时显示,取消时隐藏
searchbar
。它工作正常,取消按钮在所有
iPhones
上都可见,但由于某种原因 not
iPad
上。这是什么原因造成的?

这就是我创建

searchbar
的方式:

//Create searchbar
    func createSearchBar(){

        searchBar.showsCancelButton = true
        searchBar.tintColor = UIColor(red:0.184, green:0.996, blue:0.855, alpha:1.00)
        searchBar.placeholder = "Search brands"
        searchBar.delegate = self



        searchBar.hidden =  false
        searchBar.alpha = 0

        navigationItem.titleView = searchBar
        navigationItem.setLeftBarButtonItem(menuButton, animated: true)
        navigationItem.setRightBarButtonItem(searchButtton, animated: true)


        UIView.animateWithDuration(0.5, animations: {
            self.searchBar.alpha = 1
            }, completion: { finished in
                self.searchBar.becomeFirstResponder()
        })


    }
swift ipad uisearchbar
4个回答
12
投票

面对我的同一个项目。我已经在苹果论坛上发帖,许多开发人员评论说这是一个 xcode 错误。所以我为 ipad 视图手动添加了取消按钮

 func configureCancelBarButton() {
    let cancelButton = UIBarButtonItem()
    cancelButton.image = UIImage(named: "cancelButton")
    cancelButton.action = #selector(viewController.ipadCancelButton(_:))
    cancelButton.target = self
    self.navigationItem.setRightBarButtonItem(cancelButton, animated: true)
}

我之前发布了关于这个问题的答案。 也检查一下。 :)


7
投票

https://developer.apple.com/documentation/uikit/uisearchbar/1624283-showscancelbutton

对于在 iPad 上运行的应用程序,忽略此属性的值,并且不显示取消按钮。

Apple 在文档中添加它!


0
投票

如果您不将 UISearchbar 用作 NavigationBarItem,则 UISearchbar 的取消按钮将在 iPad 中工作。


0
投票

UISearchBar 用作导航项时在 iPad 和 iPhone 上的工作方式不同。在 iPad 上,取消按钮被“后退”按钮取代,而在 iPhone 上,取消按钮仍然显示为按钮。*

  1. 在 iPad 上,如果您将 UISearchBar 用作 UINavigationBar 中的导航项,则不会显示取消按钮。但是,如果您在任何其他上下文中使用 UISearchBar,例如在视图控制器中,取消按钮将显示并按预期运行。

  2. 在 iPhone 上,UISearchBar 既可以用作导航项,也可以在视图控制器中使用。在这两种情况下,都会显示取消按钮并按预期运行。

  3. 当 UISearchBar 在 iPhone 上用作 navigationItem 时,当用户开始编辑搜索栏时,取消按钮显示为右栏按钮项。这允许用户轻松取消搜索并返回到上一个屏幕。

    在 iPad 上,如果您使用 UISearchBar 作为 UINavigationBar 中的 navigationItem,取消按钮将不会显示。但是,如果您在任何其他上下文中使用 UISearchBar,例如在视图控制器中,取消按钮将显示并按预期运行。

    func setupCancelButton() {
    let cancelButton = UIButton(type: .system)
     // Set button title
    cancelButton.setTitle("Cancel", for: .normal)
     // Add target
    cancelButton.addTarget(self, action: #selector(cancelButtonTapped), for: .touchUpInside)
    cancelButton.translatesAutoresizingMaskIntoConstraints = false
    
    let image = UIImage(named: "cancel_icon")?.withRenderingMode(.alwaysTemplate)
    // Set image 
    cancelButton.setImage(image, for: .normal)
    cancelButton.tintColor = .black
    
    searchBar.addSubview(cancelButton)
    searchBar.setShowsCancelButton(false, animated: false)
    
    NSLayoutConstraint.activate([
        cancelButton.topAnchor.constraint(equalTo: searchBar.topAnchor, constant: 0),
        cancelButton.bottomAnchor.constraint(equalTo: searchBar.bottomAnchor, constant: 0),
        cancelButton.trailingAnchor.constraint(equalTo: searchBar.trailingAnchor, constant: -8),
        cancelButton.widthAnchor.constraint(equalToConstant: 50)
    ])
    

    } // 按钮被点击 @objc func cancelButtonTapped() { searchBar.text = "" searchBar.resignFirstResponder() }

© www.soinside.com 2019 - 2024. All rights reserved.