I'm doing a searchBar for studies, but I found some distortion in the layout in different versions of iOS, I don't remember and I don't know any relevant changes between the OS that can accomplish this, I'm a little lost.
override public func draw(_ rect: CGRect) {
drawPosition()
}
private lazy var widthLeftView: CGFloat = 0.0
private func drawPosition() {
searchView.addSubview(searchIcon)
widthConstraint = searchView.widthAnchor.constraint(equalToConstant: calculateWidthLeftView())
NSLayoutConstraint.activate([
searchView.heightAnchor.constraint(equalToConstant: searchViewSize),
widthConstraint,
searchIcon.heightAnchor.constraint(equalToConstant: searchIconSize),
searchIcon.widthAnchor.constraint(equalToConstant: searchIconSize),
searchIcon.centerYAnchor.constraint(equalTo: searchView.centerYAnchor),
searchIcon.trailingAnchor.constraint(
equalTo: searchView.trailingAnchor,
constant: 0)
])
searchBarTextField.leftView = searchView
searchBarTextField.leftViewMode = .always
}
private func calculateWidthLeftView() -> Double {
self.layoutIfNeeded()
let widht = self.frame.size.width - 54
let widhLeft = widht / 2
return widhLeft
}
Problem fixed by removing the override draw() function and putting it to be called inside the override layoutSubViews
public override func layoutSubviews() {
super.layoutSubviews()
drawPosition()
}
This decision was good for two reasons, first it fixed the bug, and second, I found that overwriting draw() impacts performance and fills up the iPhone's memory little by little,
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.