gpt4 book ai didi

swift - 当 UISearchController 聚焦时重置 UIView

转载 作者:行者123 更新时间:2023-11-30 11:22:19 26 4
gpt4 key购买 nike

我遇到一个问题,我可以在 UIViewController 中看到黑色部分,其中有 UISearchControllerUICollectionView 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"]
}

添加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 中的更改移动它。

谁能告诉我我该怎么办?

提前谢谢您。

最佳答案

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

您要么需要禁用此功能,以便 View 约束在演示过程中不会发生变化,要么需要修改约束,以便 View 能够适应导航栏的高度变化。通过为主视图的安全区域设置顶部约束,您应该能够很容易地做到这一点。

关于swift - 当 UISearchController 聚焦时重置 UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51131678/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com