Reputation: 16777
import UIKit
@IBDesignable
class LargeButtonWithIcon: UIView {
var iconBackgroundView: UIView?
var iconIv: UIImageView?
@IBInspectable var iconImage: UIImage? {
didSet {
self.iconIv?.image = iconImage
}
}
override init(frame: CGRect) {
super.init(frame: frame)
initializeView()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
initializeView()
}
override func prepareForInterfaceBuilder() {
super.prepareForInterfaceBuilder()
initializeView()
}
private func initializeView() {
initializeIconView()
}
private func initializeIconView() {
iconBackgroundView = UIView(frame: CGRect(x: 0, y: 0, width: self.frame.height, height: self.frame.height))
addSubview(iconBackgroundView!)
iconBackgroundView?.leadingAnchor.constraint(equalTo: self.leadingAnchor).isActive = true
iconBackgroundView?.topAnchor.constraint(equalTo: self.topAnchor).isActive = true
iconBackgroundView?.bottomAnchor.constraint(equalTo: self.bottomAnchor).isActive = true
iconIv = UIImageView()
iconIv?.image = iconImage
iconIv?.clipsToBounds = true
iconIv?.contentMode = .scaleAspectFit
iconIv?.translatesAutoresizingMaskIntoConstraints = false
iconBackgroundView!.addSubview(iconIv!)
iconBackgroundView?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true
// iconBackgroundView?.topAnchor.constraint(equalTo: iconBackgroundView!.topAnchor, constant: 0).isActive = true
// iconBackgroundView?.rightAnchor.constraint(equalTo: iconBackgroundView!.rightAnchor, constant: 20).isActive = true
// iconBackgroundView?.bottomAnchor.constraint(equalTo: iconBackgroundView!.bottomAnchor, constant: 20).isActive = true
// iconBackgroundView?.centerXAnchor.constraint(equalTo: iconBackgroundView!.centerXAnchor).isActive = true
// iconBackgroundView?.centerYAnchor.constraint(equalTo: iconBackgroundView!.centerYAnchor).isActive = true
}
}
I'm trying to achieve the following:
iconBackgroundView
is the square at the left of the button, iconIv
is the icon in that square.
When I activate any non-zero constraint for the iconIv
(or all of them), it prints the warning:
2020-06-03 01:26:37.163392+0300 FindDifferences[5310:34823037] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x6000012bc6e0 UIView:0x7f880c0075a0.leading == UIView:0x7f880c0075a0.leading + 20 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x6000012bc6e0 UIView:0x7f880c0075a0.leading == UIView:0x7f880c0075a0.leading + 20 (active)>
But if I change the its constant to 0
, it does not. Why, and how should I center the iconIv
in this view with given padding?
Upvotes: 0
Views: 26
Reputation: 7400
This line doesn't make sense because you're trying to set a view's leading anchor equal to its own leading anchor.
iconBackgroundView?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true
I think what you actually want is this:
iconIv?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true
Upvotes: 1