重点关注UISearchController时重置UIView

时间:2018-07-02 08:13:02

标签: swift uicollectionview uinavigationbar uisearchcontroller

我遇到一个问题,由于设计不良,我可以在UIViewController中看到黑色的部分,其中有UISearchControllerUICollectionViewUISegmentControl。 br /> 我使用以下代码在UISearchController中添加了navigationbar

func setupSearchBar(){

        navigationItem.searchController = taskSearchController
        taskSearchController.searchBar.tintColor = .white
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).defaultTextAttributes = [NSAttributedStringKey.foregroundColor.rawValue: UIColor.lightGray]

        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).attributedPlaceholder = NSAttributedString(string: "Search Text", attributes: [NSAttributedStringKey.foregroundColor: UIColor.lightGray])

        if let textfield = taskSearchController.searchBar.value(forKey: "searchField") as? UITextField {
            if let backgroundview = textfield.subviews.first {
                backgroundview.backgroundColor = UIColor.init(white: 1, alpha: 1)
                backgroundview.layer.cornerRadius = 10
                backgroundview.clipsToBounds = true
            }
        }
        taskSearchController.hidesNavigationBarDuringPresentation = true
        //navigationItem.hidesSearchBarWhenScrolling = false
        //taskSearchController.searchBar.scopeButtonTitles = ["ASSIGNED TASK","CREATED TASK"]
    }

在添加UISearchController之后,我使用以下代码添加了UISegmentControlUICollectionView

func setupView(){
        //self.view.addSubview(coverView)
        self.view.addSubview(taskSegment)

    taskSegment.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
    taskSegment.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
    taskSegment.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
    taskSegment.heightAnchor.constraint(equalToConstant: 30).isActive = true

    self.view.addSubview(collectionView)
    collectionView.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
    collectionView.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
    collectionView.topAnchor.constraint(equalTo: taskSegment.bottomAnchor, constant: 10).isActive = true
    collectionView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor, constant: 5).isActive = true

}

这是我的初步观点Task Screen,很好。

这是UISearchController集中在Task Screen with black part上的样子

在这里您可以看到navigationBar何时被隐藏,UISearchController当时位于我的UISegmentUIcollectionView保持在同一位置的情况下,因此我想根据UISearchController中更改。

谁能告诉我该怎么办?

先谢谢您。

1 个答案:

答案 0 :(得分:0)

由于hideNavigationBarDuringPresentation设置为true,因此它隐藏了导航栏。这是预期的行为。

您需要禁用此设置,以便视图约束在演示过程中不会更改,或者您需要修改约束,以使视图适应导航栏的高度变化。通过在主视图的“安全”区域设置顶部约束,您应该能够轻松地做到这一点。