Add UISearchBar fix on top of a View in iOS7?

时间:2015-05-24 22:02:57

标签: ios objective-c swift

I have a UIViewController and want to add a UISearchBar fix at the top like here:

enter image description here

When I tap into the search bar I want it to look like this:

enter image description here

It should be displayed over the navigation bar.

How can I do this using InterfaceBuilder or in Code instead of a Storyboard?

2 个答案:

答案 0 :(得分:1)

如果您正在为iOS 8构建,则可以使用 @Override protected void onResume() { super.onResume(); setContentView(R.layout.activity_main); setVolumeControlStream(AudioManager.STREAM_MUSIC); //here i want to restart the ad } ,如果您要为iOS 8以下的任何内容构建,则必须使用现已弃用的UISearchController

Apple Documentation - UISearchController

Apple Documentation - UISearchDisplayController

答案 1 :(得分:0)

我认为不可能通过Interface Builder完成所有这些操作,但如果您改变主意并决定使用代码,请在全局声明var resultSearchController = UISearchController()之后将其插入viewDidLoad():

self.resultSearchController = ({
            let controller = UISearchController(searchResultsController: nil)
            controller.searchResultsUpdater = self
            controller.dimsBackgroundDuringPresentation = true
            controller.searchBar.sizeToFit()
            controller.dimsBackgroundDuringPresentation = false

            self.tableView.tableHeaderView = controller.searchBar
            self.definesPresentationContext = true
            return controller
        })()

这将完全符合您对iOS 8及更高版本的要求。

更新: 对于界面构建器,拖动UITableViewController并将其嵌入导航控制器(选择它,编辑器>嵌入>导航控制器)。将导航控制器设置为初始视图控制器。接下来,将搜索栏和搜索显示控制器拖入表视图内的TableView控制器,将其定位在表视图的顶部。你去吧