Есть следующий эталонный пример:
Есть то, что на данный момент вышло у меня:
Вопрос, как добиться этого перехода UISearchBar`а на второй гифке? (Так, что бы он съезжал наверх при нажатии на поле поиска)
Часть моего кода:
class CompilationViewController: UIViewController, UISearchBarDelegate, UISearchControllerDelegate, UISearchResultsUpdating {
// ...
private var lastContentOffset: CGFloat = 0
private var isVisible = true
private var constraints = [NSLayoutConstraint]()
@IBOutlet var collection: UICollectionView!
@IBOutlet var logo: UIImageView!
@IBOutlet var segmentControl: UISegmentedControl!
private var searchController: UISearchController!
private var searchView: UIView!
override func loadView() {
super.loadView()
for item in view.subviews {
item.translatesAutoresizingMaskIntoConstraints = false
}
searchController = UISearchController(searchResultsController: nil)
searchController.searchResultsUpdater = self
searchController.obscuresBackgroundDuringPresentation = false
searchController.searchBar.delegate = self
searchController.searchBar.searchBarStyle = .minimal
searchView = UIView()
searchView.addSubview(searchController.searchBar)
//searchController.searchBar.sizeToFit()
view.addSubview(searchView)
searchView.translatesAutoresizingMaskIntoConstraints = false
constraints.append(logo.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor))
constraints[0].isActive = true
logo.widthAnchor.constraint(equalToConstant: 160).isActive = true
logo.heightAnchor.constraint(equalToConstant: 38).isActive = true
logo.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
constraints.append(segmentControl.topAnchor.constraintEqualToSystemSpacingBelow(logo.bottomAnchor, multiplier: 1))
constraints[1].isActive = true
segmentControl.widthAnchor.constraint(equalTo: view.safeAreaLayoutGuide.widthAnchor, multiplier: 1, constant: -16).isActive = true
segmentControl.centerXAnchor.constraint(equalTo: view.safeAreaLayoutGuide.centerXAnchor).isActive = true
constraints.append(searchView.topAnchor.constraintEqualToSystemSpacingBelow(segmentControl.bottomAnchor, multiplier: 1))
constraints[2].isActive = true
searchView.leadingAnchor.constraint(equalTo: view.leadingAnchor).isActive = true
searchView.trailingAnchor.constraint(equalTo: view.trailingAnchor).isActive = true
searchView.heightAnchor.constraint(equalToConstant: 56).isActive = true
constraints.append(collection.topAnchor.constraintEqualToSystemSpacingBelow(searchView.bottomAnchor, multiplier: 1))
constraints[3].isActive = true
collection.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor).isActive = true
collection.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor).isActive = true
collection.bottomAnchor.constraint(equalTo: view.bottomAnchor).isActive = true
}
override func viewDidLoad() {
super.viewDidLoad()
}