我遇到一个问题,由于设计不良,我可以在UIViewController
中看到黑色的部分,其中有UISearchController
,UICollectionView
和UISegmentControl
。 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
之后,我使用以下代码添加了UISegmentControl
和UICollectionView
:
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
}
在这里您可以看到navigationBar
何时被隐藏,UISearchController
当时位于我的UISegment
和UIcollectionView
保持在同一位置的情况下,因此我想根据UISearchController
中更改。
谁能告诉我该怎么办?
先谢谢您。
答案 0 :(得分:0)
由于hideNavigationBarDuringPresentation设置为true,因此它隐藏了导航栏。这是预期的行为。
您需要禁用此设置,以便视图约束在演示过程中不会更改,或者您需要修改约束,以使视图适应导航栏的高度变化。通过在主视图的“安全”区域设置顶部约束,您应该能够轻松地做到这一点。