[TIL] Searchbar In Navigationbar(네비게이션바에 서치바 넣기)

- 2 mins

2018/10/04

Searchbar in Navigationbar(네비게이션바에 서치바 넣기)

순서

#1. SearchVC-SearchResultVC 설정

  1. SearchVC 생성 및 스토리보드상 원하는 SearchResultVC 구현
  2. SearchVC 내 SearchVC 변수 선언 Ex) private var searchVC: UISearchController!
  3. viewDidLoad()함수 내 코드 *나는 setSearchViewController()함수로 뺐다.
      guard let searchResultVC = storyboard?.instantiateViewController(withIdentifier: "SearchResultVC") as? SearchResultViewController else { return }
      searchVC = UISearchController(searchResultsController: searchResultVC)
      searchVC.delegate = self
      searchVC.searchResultsUpdater = searchResultVC
      searchVC.dimsBackgroundDuringPresentation = true
      definesPresentationContext = true
      searchVC.loadViewIfNeeded()
      searchVC.searchBar.delegate = searchResultVC
      searchVC.hidesNavigationBarDuringPresentation = false
      // 서치바 관련 폰트와 색상 관련 설정을 마친 후
      navigationItem.titleView = searchVC.searchBar
    
    • TMI : 기존 DrinKit이라는 개인앱에서 SearchBar를 구현해봤으나, UIView를 생성해 추가했었다. Navigationbar에 SearchBar를 넣는 작업은 StoryBoard상에서 찾아봤지만, 바로 구현하기에는 어려움이 있었다.
    • 고로, 위 코드에서 내가 가장 알고자 했던 코드 : navigationItem.titleView = searchVC.searchBar

#2. Searchbar의 Placeholder 설정

#3. Searchbar의 Color 설정

#4. SearchBar의 Icon 편집

comments powered by Disqus
rss facebook twitter github youtube mail spotify lastfm instagram linkedin google google-plus pinterest medium vimeo stackoverflow reddit quora quora