首页 文章

使用导航栏和iMessage应用程序

提问于
浏览
1

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

1 回答

  • 0

    编辑:我没有登上iOS10并且不了解iMessage应用程序 . 你的问题现在更有意义 . 无论如何,我会将原来的答案留在这里 .


    这个项目包含来自apple的原始iMessages应用程序的 basics . 当然还有 a lot more 来调整,但它应该让你开始 .

    显示带有消息的表格视图,除非您向上滚动查找它,然后单击导航栏隐藏的搜索栏并且搜索栏也显示取消按钮,否则搜索栏最初是隐藏的 .

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

    enter image description here

    enter image description here

    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()
    
    }
    
    }
    

相关问题