代码之家  ›  专栏  ›  技术社区  ›  Noodledew

当启用UIButton的spacing或contentEdgeInsets时,UIStackViews Autolayout将抛出

  •  0
  • Noodledew  · 技术社区  · 7 年前

    我有一个 UIStackView 参与按钮

    这些按钮只包含一个图像。为了在顶部和底部有一些填充物,我设置了 UIEdgeInsets .spacing = 1 财产似乎不能同时发挥作用。为什么?


    堆栈视图:

    class ParticipateButtonStackView: UIStackView {
        //MARK: - Properties & Variables
        var delegate: participateButtonStackViewDelegate?
    
        //MARK: - GUI Objects
        var buttons: [ParticipateButton]!
        let sureButton = ParticipateButton(type: .sure)
        let maybeButton = ParticipateButton(type: .maybe)
        let nopeButton = ParticipateButton(type: .nope)
    
        //MARK: - Init & View Loading
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.addBackground(color: UIColor.gray)
            buttons = [sureButton, maybeButton, nopeButton]
            setupStackView()
            setupButtons()
        }
    
        //MARK: - Setup
        func setupStackView() {
            self.axis           = .horizontal
            self.spacing        = 1
            self.alignment      = .leading
            self.distribution   = .fillEqually
        }
    
        func setupButtons() {
            for button in buttons {
                addArrangedSubview(button)
                button.addTarget(self, action: #selector (self.buttonClick(sender:)), for: .touchUpInside)
            }
        }
    }
    

    参与按钮

    class ParticipateButton: UIButton {
    
        var typeOfButton: participateLists!
    
        override init(frame: CGRect) {
            super.init(frame: frame)
            self.backgroundColor  = LebensfitSettings.Colors.buttonBG
            self.tintColor        = LebensfitSettings.Colors.basicTintColor
            self.imageView?.contentMode = .scaleAspectFit
            self.contentEdgeInsets = UIEdgeInsets(top: 7, left: 0, bottom: 7, right: 0)
        }
    
        convenience init(type: participateLists) {
            self.init()
            self.typeOfButton = type
            setImage(type: type)
        }
    
        func setImage(type: participateLists) {
            var buttonImage: UIImage?
            switch type {
            case .sure:
                buttonImage = UIImage(named: "pb_sure")?.withRenderingMode(.alwaysTemplate)
            [...]
            }
            self.setImage(buttonImage, for: .normal)
        }
    }
    

    将尝试通过打破约束来恢复

    编辑:

    participateButtonStackView.leftAnchor = view.leftAnchor
    participateButtonStackView.rightAnchor = view.rightAnchor
    participateButtonStackView.bottomAnchor = view.bottomAnchor
    participateButtonStackView.heightAnchor = equalToConstant(50)
    
    1 回复  |  直到 7 年前
        1
  •  0
  •   Noodledew    7 年前

    func setBordersLeftRight() {
        let borderLeft = UIView()
        let borderRight = UIView()
        borderLeft.backgroundColor = .gray
        borderRight.backgroundColor = .gray
    
        addSubview(borderLeft)
        addSubview(borderRight)
        borderLeft.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0.5, height: 0)
        borderRight.anchor(top: topAnchor, left: nil, bottom: bottomAnchor, right: rightAnchor, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: 0.5, height: 0)
    }