Swift 聚焦UISearchController时重置UIView
我正面临一个问题,我可以在我的Swift 聚焦UISearchController时重置UIView,swift,uicollectionview,uinavigationbar,uisearchcontroller,Swift,Uicollectionview,Uinavigationbar,Uisearchcontroller,我正面临一个问题,我可以在我的UIViewController中看到一个黑色部分,其中我有一个UISearchController,UICollectionView和UISegmentControl,因为我的设计很差。 我使用以下代码在navigationbar中添加了UISearchController: func setupSearchBar(){ navigationItem.searchController = taskSearchController
UIViewController
中看到一个黑色部分,其中我有一个UISearchController
,UICollectionView
和UISegmentControl
,因为我的设计很差。我使用以下代码在
navigationbar
中添加了UISearchController
:
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"]
}
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
}
添加UISearchController
后,我将使用以下代码添加UISegmentControl
和UICollectionView
:
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"]
}
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
}
这是我最初的看法,很好
这就是聚焦于UISearchController
时的外观
在这里,您可以看到当navigationBar
隐藏和UISearchController
放置在顶部时,我的UISegment
和UIcollectionView
保持在相同的位置,因此我想根据UISearchController
中的更改来移动它
谁能告诉我该怎么办
提前感谢。它会隐藏导航栏,因为已设置为true。这是故意的行为
您需要禁用此选项,以便视图约束在演示过程中不会更改,或者需要修改约束,以便视图能够根据导航栏的高度更改进行调整。通过将顶部约束设置到主视图的安全区域,您应该能够非常轻松地完成此操作 我已经使用iOS11中的自动布局检查将
UISegmentControl
设置为视图的安全区域。搜索栏(如navigationItem.searchController=taskSearchController
)默认隐藏,向下拖动时显示。你有没有做些什么来推翻这个?当以编程方式添加约束时,您应该设置translatesAutoresizingMaskIntoConstraints=false
黑屏是窗口的颜色,如果我将窗口颜色设置为白色,则黑屏将变为白色。这解决了一个问题。但间距问题仍然存在,我需要将CollectionView推到搜索栏。