将UISearchController添加到UIView

时间:2015-09-24 16:03:23

标签: ios swift uiview

我尝试向广告UIViewSearchController添加UIView,但似乎没有显示?到目前为止,我已经创建了UISearchControllerUISearchBar的子类。我已经完成了从searchController中删除取消按钮的操作。但是,由于我添加了这些自定义类,因此searchBar不会显示在View中?怎么会这样呢?

viewcontroller中的viewDidLoad

    //SearchController
    backSearchView.backgroundColor = UIColor.clearColor()
    self.resultSearchController = ({
        let controller = MySearchController(searchResultsController: nil)
        controller.hidesNavigationBarDuringPresentation = false
        controller.dimsBackgroundDuringPresentation = false

        controller.searchResultsUpdater = self
        controller.dimsBackgroundDuringPresentation = false
        controller.searchBar.sizeToFit()
        controller.searchBar.frame = CGRectMake(8, 8, self.view.frame.width-16, 44)
        controller.searchBar.setShowsCancelButton(false, animated: false)

        controller.searchBar.backgroundImage = UIImage()
        controller.searchBar.placeholder = "Søg"
        controller.searchBar.returnKeyType = UIReturnKeyType.Default
        controller.searchBar.delegate = self
        let textFieldInsideSearchBar = controller.searchBar.valueForKey("searchField") as? UITextField
        textFieldInsideSearchBar?.backgroundColor = UIColor.whiteColor()
        textFieldInsideSearchBar?.clipsToBounds = true
        textFieldInsideSearchBar?.layer.cornerRadius = 1
        textFieldInsideSearchBar?.layer.borderWidth = 2
        textFieldInsideSearchBar?.layer.borderColor = UIColor.whiteColor().CGColor
        textFieldInsideSearchBar?.textColor = UIColor.darkGrayColor()
        backSearchView.addSubview(controller.searchBar)



        return controller
    })()

SearchController子类

class MySearchController: UISearchController, UISearchBarDelegate {

        lazy var customSearchBar: NoCancelSearchBar = {
            [unowned self] in
            let result = NoCancelSearchBar(frame:CGRectZero)
            result.delegate = self
            return result
            }()

        override var searchBar: UISearchBar {
            get {
                return customSearchBar
            }
        }

        override func viewDidLoad() {
            super.viewDidLoad()

            // Do any additional setup after loading the view.
        }

        override func didReceiveMemoryWarning() {
            super.didReceiveMemoryWarning()
            // Dispose of any resources that can be recreated.
        }


    }

SearchBar子类

class NoCancelSearchBar: UISearchBar {

    override func layoutSubviews() {
        self.showsCancelButton = false
    }

}

0 个答案:

没有答案