Since viewIsAppearing() appeared, I am using addSubview() in viewDidLoad() and applying Constraint in viewIsAppearing().
I placed the SearchBar at the bottom of the screen, and used UIKeyboardLayoutGuide to move the SearchBar according to the position of the keyboard. It is similar to the search bar in Safari.
There was no problem with iOS 17.0 or higher based on the simulator. Also, there was no problem with version 16.4 of the simulator.
-----
At this time, the problem is as follows:
However, as a result of using the 15.0 and 15.2 versions of the simulator, the SearchBar did not appear on the screen.
When it was fixed to the bottom of the screen based on the safearea without using KeyboardLayoutGuide, it appeared on the screen normally. So I thought there was a problem with UIKeyboardLayoutGuide.
Finally, when I moved the code specifying the layout of the SearchBar that applied UIKeyboardLayoutGuide from viewIsAppearing() to viewDidLoad(), I confirmed that it appeared on the screen normally.
-----
But I don't understand this behavior. The minimum supported version of UIKeyboardLayoutGuide is iOS 15.0. Additionally, the minimum supported version of viewIsAppearing() is iOS 13.0.
Could you tell me what I missed?
override func viewDidLoad() { super.viewDidLoad() addSubviews() layout() // appeared searchBar } // ====================================== override func viewDidLoad() { super.viewDidLoad() addSubviews() } override func viewIsAppearing(_ animated: Bool) { super.viewIsAppearing(animated) layout() // not appeared searchBar } // ====================================== func layout() { // SearchBar layout Code let searchBarHeight = 50 collectionView.snp.makeConstraints { $0.top.equalTo(view.safeAreaLayoutGuide) $0.horizontalEdges.equalTo(view.safeAreaLayoutGuide) $0.bottom.equalTo(view.safeAreaLayoutGuide).offset(-searchBarHeight) } searchBar.snp.makeConstraints { $0.height.equalTo(searchBarHeight) $0.left.equalTo(view.safeAreaLayoutGuide) $0.bottom.equalTo(view.keyboardLayoutGuide.snp.top) } registButton.snp.makeConstraints { $0.height.equalTo(searchBarHeight) $0.left.equalTo(searchBar.snp.right) $0.right.equalTo(view.safeAreaLayoutGuide) $0.bottom.equalTo(view.keyboardLayoutGuide.snp.top) $0.width.equalTo(100) } } =================
I would like to add the additional facts I just noticed.
When the layout of the SearchBar performed in viewDidLoad() returns after switching to the Navigation screen, the SearchBar is not visible.
I used viewWillAppear() to solve this problem.
======= This is code for Test (Set simulator version to 15.0)
import UIKit import SnapKit final class ViewController: UIViewController { private let uiTextField: UITextField = { $0.placeholder = "search" $0.backgroundColor = .red return $0 }(UITextField()) override func viewIsAppearing(_ animated: Bool) { super.viewIsAppearing(animated) view.backgroundColor = .white layout() } private func layout() { view.addSubview(uiTextField) uiTextField.snp.makeConstraints { $0.height.equalTo(50) $0.horizontalEdges.equalToSuperview() $0.bottom.equalTo(view.keyboardLayoutGuide.snp.top) } } override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) } }
layoutcall takes place; it's that your basic constraints are defective.registButtonin your code, and I did test myself and couldn't reproduce any issue. If you don't show me the real code I have no way to reproduce exactly your situation. I pointed that out before too. Maybe it's simply a matter sendingsetNeedsLayout()to the search bar's superview, but I can't test that if I can't reproduce the problem.