在modalPresentationStyle中快速隐藏UIView

时间:2018-07-30 07:15:21

标签: ios swift

我有一个UIViewController作为modalPresentationStyle,其中有一个TextView和一个Button(Button扩展了一个UIView,其中SearchBar在其中)。现在的问题是,我尝试尝试再次按下SearchBar的UIView时关闭它,我已经尝试找到一种解决方案,但不幸的是没有成功。

@IBAction func showSearchbar(_ sender: UIBarButtonItem) {


    let searchbarView = UIView(frame: CGRect(x: 0, y: 44, width: self.view.frame.width, height: 50))
    searchbarView.backgroundColor = UIColor.gray
    textNotiz.textContainerInset = UIEdgeInsets(top: 50, left: 0, bottom: 0, right: 0)
    self.view.addSubview(searchbarView)

    let searchBar:UISearchBar = UISearchBar(frame: CGRect(x: 0, y: 0, width: searchbarView.frame.width, height: 50))

    searchBar.delegate = self
    searchBar.searchBarStyle = UISearchBarStyle.prominent
    searchBar.placeholder = " Search..."
    searchBar.sizeToFit()
    searchBar.isTranslucent = false
    searchBar.backgroundColor = UIColor.lightGray
    searchbarView.addSubview(searchBar)

    if isExpanded == false  {

        isExpanded = true


    } else {

        isExpanded = false
        searchbarView.isHidden = true
        textNotiz.textContainerInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

    }
}

展开搜索栏/视图之前 Before Searchbar/View is Expanded

展开搜索栏/视图后 After Searchbar/View is Expanded

1 个答案:

答案 0 :(得分:0)

好的,我解决了这个问题,我没有全局设置UISearchbar和UIView的变量,因此UIView不会消失。