2014-11-03 3 views
4

iOS 8에 도입 된 새로운 UISearchController API를 실험하고 있습니다. API가 새롭지 만 동일한 이전 UISearchBar을 사용합니다. UINavigationControllertitleView에 추가했습니다.UINavigationBar 내부에 포함 된 UISearchBar 너비가 변경되지 않습니다.

enter image description here enter image description here

import UIKit 

class ViewController: UIViewController, UISearchBarDelegate { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let searchController = UISearchController(searchResultsController: nil) 
     searchController.hidesNavigationBarDuringPresentation = false 
     searchController.dimsBackgroundDuringPresentation = false 
     searchController.searchBar.delegate = self 
     navigationItem.titleView = searchController.searchBar 
    } 
} 

나는 탐색 모음의 전체 폭을 검색 창을 필요가 없습니다. 문제는 크기를 조정할 수 없다는 것입니다. 먼저 검색 창 프레임을 설정해 보았습니다.

searchController.searchBar.frame = CGRect(x: 0, y: 0, width: 100, height: 44) 

그렇기 때문에 작동하지 않아 titleView의 프레임을 설정하려고했습니다.

navigationItem.titleView!.frame = CGRect(x: 0, y: 0, width: 100, height: 44) 

둘 중 어느 것도 작동하지 않습니다.

누구든지이 작업을 수행 할 다른 방법을 알고 있습니까?

감사합니다.

답변

14

다른보기를 탐색 모음 제목으로 사용하고 검색 막대 제목 안에 배치 할 수 있습니다. 막대의 색조 문제가 발생하면 backgroundImage 속성을 설정하여 해결할 수 있습니다. 작동해야 함 :

let searchController = UISearchController(searchResultsController: nil) 
searchController.hidesNavigationBarDuringPresentation = false 
searchController.dimsBackgroundDuringPresentation = false 
searchController.searchBar.delegate = self 

let frame = CGRect(x: 0, y: 0, width: 100, height: 44) 
let titleView = UIView(frame: frame) 
searchController.searchBar.backgroundImage = UIImage() 
searchController.searchBar.frame = frame 
titleView.addSubview(searchController.searchBar) 
navigationItem.titleView = titleView 
+0

위대한 작품! 고맙습니다. – Isuru

+0

도와 주셔서 감사합니다! 잘 했어! :) –

+0

완벽한 작업. 감사! –

관련 문제