2015-09-20 63 views
0

後找錯了,我加入編程UISearchControllerUIViewController,當UIViewController加載一切都很好,但是當我在UISearchController觸摸UISearchBar,將其移到底部,它正在尋找錯誤的。我第一次在代碼中添加UI元素。UISearchController被觸摸

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.definesPresentationContext = true 
    searchController = ({ 
     let controllerSearch = UISearchController(searchResultsController: nil) 
     controllerSearch.searchBar.frame = CGRectMake(0, 20, self.view.frame.size.width, 44) 
     controllerSearch.delegate = self 
     controllerSearch.searchBar.delegate = self 
     controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Default 
     controllerSearch.dimsBackgroundDuringPresentation = false 
     controllerSearch.hidesNavigationBarDuringPresentation = false 
     controllerSearch.searchResultsUpdater = self 
     controllerSearch.searchBar.sizeToFit() 
     self.view.addSubview(controllerSearch.searchBar) 
     return controllerSearch 
    })() 
} 

雖然我做的是控制檯顯示的UISearchController框架,它顯示了正確的結果(0.0,20.0,320.0,44.0),請幫助我的人。 我仍然嘗試過,但結果是一樣的。

let frame = CGRectMake(0, 20, self.view.frame.size.width, 44) 
    controllerSearch.searchBar.frame = frame 

enter image description here enter image description here

回答

0

我解決我的問題。我增加了以下元素

@IBOutlet weak var navigationItemSearch: UINavigationItem! 

而且改變了我的UISearchController

searchController = ({ 
     let controllerSearch = UISearchController(searchResultsController: nil) 
     controllerSearch.delegate = self 
     controllerSearch.searchBar.delegate = self 
     controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Prominent 
     controllerSearch.dimsBackgroundDuringPresentation = false 
     controllerSearch.hidesNavigationBarDuringPresentation = false // false 
     controllerSearch.searchBar.searchBarStyle = UISearchBarStyle.Default 
     controllerSearch.searchResultsUpdater = self 
     // placing 
     controllerSearch.searchBar.sizeToFit() 
     navigationItemSearch.titleView = controllerSearch.searchBar 
     // 
     controllerSearch.searchBar.spellCheckingType = UITextSpellCheckingType.Yes 
     controllerSearch.searchBar.autocapitalizationType = UITextAutocapitalizationType.None 
     controllerSearch.searchBar.keyboardType = UIKeyboardType.WebSearch 
     return controllerSearch 
    })()