2015-09-11 34 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 
} 

希望这可以帮助任何一个。