使用导航栏和iMessage应用程序

时间:2016-08-21 21:21:10

标签: ios autolayout uinavigationbar uisearchcontroller imessage

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

1 个答案:

答案 0 :(得分:0)

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

这是一个项目,其中包含来自apple的原始iMessages应用的 基础 。当然还有 更多 来调整,但它应该让你开始。

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

如果您想下载整个项目,以便了解我如何设置故事板,您可以在此处下载项目。 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()

}

}