繁体   English   中英

以编程方式添加任何非零约束时无法同时满足约束

[英]Unable to simultaneously satisfy constraints when adding any non-zero constraint programmatically

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
    }

}

我正在努力实现以下目标:

按钮

iconBackgroundView是按钮左侧的正方形, iconIv是该正方形中的图标。

当我为iconIv (或全部)激活任何非零约束时,它会打印警告:

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)>

但是,如果我将其常量更改为0 ,则不会。 为什么,我应该如何在这个视图中使用给定的填充将iconIv

这条线没有意义,因为您试图将视图的前导锚设置为等于它自己的前导锚。

iconBackgroundView?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true

我认为您真正想要的是:

iconIv?.leadingAnchor.constraint(equalTo: iconBackgroundView!.leadingAnchor, constant: 20).isActive = true

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM