Reputation: 143
I programically create the button:
final class SearchArea: UIView {
private let offset: CGFloat = 5
private(set) lazy var searchRequest = {
let item = UITextField()
item.backgroundColor = .systemBackground
item.borderStyle = .roundedRect
item.clearButtonMode = .always
item.layer.cornerRadius = offset
return item
}()
private(set) lazy var searchButton = {
let item = UIButton()
item.backgroundColor = .systemBlue
item.layer.cornerRadius = offset
item.setImage(UIImage(systemName: "magnifyingglass"), for: .normal)
return item
}()
func setupView() {
backgroundColor = .systemBackground
let safeArea = self.safeAreaLayoutGuide
addSubview(searchButton)
searchButton.addConstraints(
.alignTopWithTop(of: safeArea, const: offset),
.alignBottomWithBottom(of: safeArea, const: offset),
.alignTrailingWithTrailing(of: safeArea, const: offset),
.setAspectWidth(ratio: 1)
)
addSubview(searchRequest)
searchRequest.addConstraints(
.alignTopWithTop(of: safeArea, const: offset),
.alignBottomWithBottom(of: safeArea, const: offset),
.alignLeadingWithLeading(of: safeArea, const: offset),
.alignTrailingWithLeading(of: searchButton, const: offset)
)
}
}
But get a strange behavior - icon is shown only when the button is pressed, all other time it doesn't have image. But it should due to Apple manual:
static var normal: UIControl.State The normal, or default, state of a control where the control is enabled but neither selected nor highlighted.
So item.setImage(UIImage for: .normal state should work ok...
Upvotes: 0
Views: 36
Reputation: 167
This because the button have a default tint color UIColor.systemBlue
, So can NOT see the image with same background.
Try to differentiate button Tint & Background color
Upvotes: 0