无法设置具有视图层次结构的布局,无法限制约束

时间:2016-02-16 10:22:40

标签: ios swift uiview autolayout

我正在尝试建立这样的东西:

trigger()

这是我视图上方的红色主按钮。如果我点击按钮,则会出现彼此间距固定的其他按钮。添加第二个菜单项时,我遇到autolayout问题。我的建议是,当我尝试添加第二个时,第一个菜单项尚未准备好进行自动布局。控制台说:

'无法设置视图层次结构的布局,无法用于约束。'

在添加下一个项目之前,如何确保一切准备就绪?

以下是我添加菜单项的方法:

let socialMenuButton1 = SocialMenuButton(parentView: controller.view, predecessor: mainButton!, title: "Menu1", image: UIImage(named: "icon")!, highlightedImage: UIImage(named: "icon-highlighted")!) { () -> Void in
            self.showAlert("Menu1")
            }
        self.menuButtons.append(socialMenuButton1)

        /*
        let socialMenuButton2 = SocialMenuButton(parentView: controller.view, predecessor: menuButtons[0], title: "Menu2", image: UIImage(named: "icon")!, highlightedImage: UIImage(named: "icon-highlighted")!) { () -> Void in
            self.showAlert("Menu2")
        }
        self.menuButtons.append(socialMenuButton2)

这里是menuItem类:

class SocialMenuButton: UIView {


    private var mainButton : UIButton?
    private var textLabel : UILabel?
    weak var delegate: SocialButton?
    private var tappedAction: (() -> Void)?


    init(parentView: UIView, predecessor: UIView, title: String?, image: UIImage, highlightedImage: UIImage, itemTapped: (() -> Void)?) {

        let itemFrame = CGRect(x: 0.0, y: 0.0, width: image.size.width, height: image.size.height)

        super.init(frame: itemFrame)
        mainButton = UIButton(frame: CGRect(x:0, y:0, width: 50, height: 50))

        if let mainButton = mainButton {
            mainButton.setImage(image , forState: UIControlState.Normal)
            mainButton.setImage(highlightedImage , forState: UIControlState.Highlighted)
            mainButton.backgroundColor = UIColor.greenColor()
            parentView.addSubview(mainButton)

            mainButton.translatesAutoresizingMaskIntoConstraints = false

            let widthConstraint = NSLayoutConstraint(item: mainButton, attribute:
                .Width , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60)
            parentView.addConstraint(widthConstraint)

            let heightConstraint = NSLayoutConstraint(item: mainButton, attribute:
                .Height , relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1.0, constant: 60)
            parentView.addConstraint(heightConstraint)

            let bottomConstraint = NSLayoutConstraint(item: mainButton, attribute:
                .Bottom, relatedBy: .Equal, toItem: predecessor, attribute: .Top, multiplier: 1.0, constant: -50)

            parentView.addConstraint(bottomConstraint)


            let trailingConstraint = NSLayoutConstraint(item: mainButton, attribute:
                .Trailing, relatedBy: .Equal, toItem: predecessor, attribute: .Trailing, multiplier: 1.0, constant: 0)
            parentView.addConstraint(trailingConstraint)
        }

        textLabel = UILabel(frame: CGRect(x:0, y:0, width: 100, height: 30))

        if let textLabel = textLabel{
            textLabel.text = title
            textLabel.backgroundColor = UIColor.greenColor()
            parentView.addSubview(textLabel)


            textLabel.translatesAutoresizingMaskIntoConstraints = false

            let trailingConstraint = NSLayoutConstraint(item: textLabel, attribute:
            .Trailing, relatedBy: .Equal, toItem: mainButton, attribute: .Leading, multiplier: 1.0, constant: -30)
            parentView.addConstraint(trailingConstraint)


            let centerYConstraint = NSLayoutConstraint(item: textLabel, attribute:
            .CenterY, relatedBy: .Equal, toItem: mainButton, attribute: .CenterY, multiplier: 1.0, constant: 0)
            parentView.addConstraint(centerYConstraint)
        }

        self.tappedAction = itemTapped

    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
    }

    // MARK: - Tapped Action
    func tapped() {
        self.delegate?.menuItemTapped(self)
        self.tappedAction?()
    }

}

0 个答案:

没有答案
相关问题