How to add UISearchBar to NavigationBar through the storyboard in iOS

允我心安 提交于 2019-12-04 03:49:01

问题


I find what seems like an elegant answer to this question at How to add search bar in navigation bar for iPhone (one question-one answer: very short so please look).

I like the piece about doing it in the storyboard (aka IB): i.e. "create an outlet from the NavigationBar titleView to the SearchBar in IB". But where would I place the SearchBar on the storyboard?

Here is what I did:

  • I dropped the SearchBar in my TableView header
  • Then from NavigatorBar I point outlet to the SearchBar as titleView (the only option)
  • But when I run the program I get a crash: terminating with uncaught exception of type NSException.

The program also opened my code in

 int main(int argc, char * argv[])
 {
    @autoreleasepool {
        return UIApplicationMain(argc, argv, nil, NSStringFromClass([MINAppDelegate class]));
    }
 }

回答1:


just drag a View (UIView) into navigation bar, then you can drag almost any UI element inside the view, including UISearchBar. I tried it and works for me.



来源:https://stackoverflow.com/questions/24808818/how-to-add-uisearchbar-to-navigationbar-through-the-storyboard-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!