2015-09-11 68 views
1

我試圖將SearchController添加到UINavigationBar。我試圖在UINavigationController的後退按鈕後設置UISearchBar的UITextField。我希望有更多的空間後退按鈕在UISearchBar中設置textfield

enter image description here

後,當我開始尋找它顯示爲

enter image description here

而我應該能夠查看後退按鈕。只有文本字段寬度應該減少。也取消後,它應該再次回到初始佈局。儘管它顯示如下:

enter image description here

下面是我的代碼

var searchResultController = UISearchController() 

self.searchResultController = ({ 

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

    controller.hidesNavigationBarDuringPresentation = false 
    self.navigationController?.navigationBar.addSubview(controller.searchBar) 

    return controller 
})() 

override func viewDidAppear(animated: Bool) { 
    for subView in searchResultController.searchBar.subviews{ 
     for subsubView in subView.subviews { 
      if let textField = subsubView as? UITextField { 
       var bounds: CGRect 
       bounds = textField.frame 
       bounds.size.width = self.view.bounds.width - 50  
      } 
     } 

    } 
} 

請讓我知道我可以解決這個問題。

在此先感謝

回答

1

有關設置的UISearchBar它添加到導航的titleview的作爲

self.navigationItem.titleView = controller.searchBar 

爲了去除取消按鈕,我們可以使用UISearchControllerDelegate方法

func didPresentSearchController(searchController: UISearchController) { 
    searchController.searchBar.showsCancelButton = false 
} 

希望這可以幫助任何一個。