ios - 搜索栏从 iPhone 上的“搜索”按钮展开

标签 ios iphone swift

当我单击搜索按钮时,如何使我的搜索栏在导航栏中展开,就像 Twitter 应用程序中的那样?现在我的搜索栏刚好落在导航栏的顶部。这是我在单击导航栏中的搜索按钮时调用的函数:

func searchbuttonclicked(sender: UIBarButtonItem) {
    var searchresults = storyboard?.instantiateViewControllerWithIdentifier("searchresults") as searchResultsController

    var searchcontroller = UISearchController(searchResultsController: searchresults)
    searchcontroller.hidesNavigationBarDuringPresentation = false
    searchcontroller.searchResultsUpdater = searchresults
    searchcontroller.modalTransitionStyle = .CrossDissolve

    presentViewController(searchcontroller, animated: true, completion: nil)
}

最佳答案

来自 this answer ,你可以使用类似的东西:

// declare as property
var searchBar: UISearchBar!

// in viewDidLoad
searchBar = UISearchBar(frame: CGRectMake(0.0, -80.0, 320.0, 44.0))
navigationController?.navigationBar.addSubview(searchBar)

// in action to show
searchBar.frame = CGRectMake(0.0, 0, 320, 44)

// in viewWillDisappear
searchBar.removeFromSuperview()

但是,UISearchController 上的 searchBar 属性是只读的,因此您需要子类化 UISearchController 以使其使用搜索导航 Controller 中的栏,而不是它默认显示的栏。

关于ios - 搜索栏从 iPhone 上的“搜索”按钮展开,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26727061/

相关文章:

jquery - JQTouch压感

iphone - 如何在没有 uitableview 的情况下创建基于导航的应用程序

iphone - 如何在 MKMapView (SDK3.2) 中使用 UIPopoverController 在 iPad 上关闭 PopoverAnimated

ios - UINavigationItem 图像始终居中

swift - SpriteKit游戏中的高CPU和帧速率下降

ios - UITableview 不滚动?

ios - SaaS 可再生订阅 : Apple and revenue share with other B2B partners

ios - Swift:对数字字符串字典进行排序

ios - OAuthException/必须使用事件访问 token 来查询有关当前用户的信息

swift - 如何使用 swift 禁用 webView 中的水平滚动?