【问题标题】:Using a Navigation bar and iMessage App使用导航栏和 iMessage 应用程序
【发布时间】:2016-08-21 21:21:09
【问题描述】:

我正在使用 UISearchBar/UISearchController 和 MKMapView 创建一个 iMessage 应用程序。搜索栏在紧凑视图中完美显示(我知道您不能在紧凑视图中设置搜索栏,但仅用于测试)固定在屏幕顶部。但是,在展开视图中,搜索栏被 iMessage 导航栏隐藏。我无法将搜索栏限制在顶部布局指南,因为导航控制器位于顶部布局指南之上。关于如何将搜索栏限制在 iMessage 顶部导航栏下方的任何想法?

【问题讨论】:

    标签: ios autolayout uinavigationbar uisearchcontroller imessage


    【解决方案1】:

    编辑: 我没有掌握 iOS10,也不了解 iMessage 应用程序。你的问题现在更有意义了。不管怎样,我会把我原来的答案写在这里。


    这是一个项目,它具有来自苹果的原始 iMessages 应用的基础。当然还有 更多 需要调整,但它应该可以帮助您入门。

    显示带有消息的表格视图,搜索栏最初是隐藏的,除非您向上滚动找到它,一旦您单击搜索栏,导航栏就会隐藏,搜索栏也会显示取消按钮。

    如果您想下载整个项目以便查看我是如何设置故事板的,您可以在此处下载该项目。 http://www.filedropper.com/forjeremykelleher

    import UIKit
    
    class TableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    
    @IBOutlet weak var tableView: UITableView!
    var messages = [Int]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        for x in 0...25 {
            messages.append(x)
        }
    
        // Start with the tableview scrolled down by 44
        // so the search bar doesn't show up only until you scroll back up
        // Like in the iMessage App.
        let height = tableView.tableHeaderView?.frame.size.height
        let pointXY = CGPoint(x: 0, y: height!)
        tableView.setContentOffset(pointXY, animated: false)
    }
    
    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell") as! TableViewCell
        cell.messageLabel.text = "Message # \(indexPath.row)"
    
        return cell
    }
    
    }
    
    extension TableViewController: UISearchBarDelegate {
    
    func searchBarTextDidBeginEditing(searchBar: UISearchBar) {
        // Hide the navigation bar when they press on search
        navigationController?.setNavigationBarHidden(true, animated: true)
        searchBar.setShowsCancelButton(true, animated: true)
    }
    
    func searchBarCancelButtonClicked(searchBar: UISearchBar) {
        navigationController?.setNavigationBarHidden(false, animated: true)
        searchBar.setShowsCancelButton(false, animated: false)
        searchBar.resignFirstResponder()
    
    }
    
    }
    

    【讨论】:

    • 我相信最初的问题是关于 iOS 10 中的新 iMessage 应用程序,而不是默认的 Messages 应用程序。
    • 这个问题现在更有意义了。
    猜你喜欢
    • 1970-01-01
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 2015-06-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多