2016-12-26 114 views
0

我有一些問題。我有UITableView和tableView的頭是searchController與1紅色視圖(這個視圖內部將是一些圖標)。 enter image description here關閉搜索欄

搜索開始後出現取消按鈕。 enter image description here

現在我按取消按鈕和搜索欄調整到全屏大小和紅色視圖消失。 enter image description here

如何解決這個問題?

這裏是我headerView類:

lazy var searchController = UISearchController(searchResultsController: nil).then { 
    //return UISearchController().then { 
     $0.dimsBackgroundDuringPresentation = false 
     $0.searchBar.placeholder = "Искать по имени" 
     $0.searchBar.tintColor = .black 
     $0.searchBar.searchBarStyle = .minimal 
     $0.searchBar.backgroundColor = .white 
     UIBarButtonItem.appearance(whenContainedInInstancesOf: [UISearchBar.self]).setTitleTextAttributes([NSFontAttributeName : UIFont.appleSystemRegular(15)], for: .normal) 
     $0.searchBar.setValue("Отмена", forKey:"_cancelButtonText") 

    } 

lazy var contentView: UIView = { 
    let view = UIView() 
    view.backgroundColor = .red 
    return view 
}() 

override init(frame: CGRect) { 
    super.init(frame: frame) 
    setupViews() 
    setupConstrains() 
} 

required init?(coder aDecoder: NSCoder) { 
    fatalError("init(coder:) has not been implemented") 
} 

fileprivate func setupViews() { 
    addSubview(contentView) 
    addSubview(searchController.searchBar) 
} 

func setupConstrains() { 
    searchController.searchBar.frame = CGRect(x: 0, y: 0, width: self.frame.width - 80, height: 44) 
    contentView.frame = CGRect(x: self.frame.width - 65, y: 0, width: 50, height: 44) 
} 
+0

您是否使用約束來安排searchBar和redView? –

+0

不,我現在試試 –

+0

你可以使用UITableViewHeaderFooterView來正確使用約束。 –

回答

0

刪除searchcontroller並嘗試搜索只是搜索欄委託。

func searchBar(searchBar: UISearchBar, textDidChange searchText: String) { 

}