Chameleon
Chameleon

Reputation: 1608

UIButton extension addSubview not working

Trying to make a UIButton that when long pressed, exposes four subviews (UIButtons - north, east, south & west) that user can then slide finger to and lift for 4 separate actions.

extension UIButton {
func EnablePopOuts (North:Bool = true, nTitle:String, nColor:UIColor = .black, nAction: Selector) {
    if North {
        let northButton = UIButton(frame: self.frame)
        northButton.translatesAutoresizingMaskIntoConstraints = false
        let nA = NSLayoutConstraint(item: northButton, attribute: .height
                                    , relatedBy: .equal, toItem: self, attribute: .height, multiplier: 1, constant: 0)
        let nB = NSLayoutConstraint(item: northButton, attribute: .width
                                    , relatedBy: .equal, toItem: self, attribute: .width, multiplier: 1, constant: 0)
        let nC = NSLayoutConstraint(item: northButton, attribute: .bottom
                                    , relatedBy: .equal, toItem: self, attribute: . top, multiplier: 1, constant: 0)
        let nD = NSLayoutConstraint(item: northButton, attribute: .leading
                                    , relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 0)
        self.addSubview(northButton)
        self.didAddSubview(northButton)
        self.addConstraints([nA,nB,nC,nD])
        self.bringSubviewToFront(northButton)
        northButton.setTitle(nTitle, for: .normal)
        northButton.backgroundColor = nColor
        northButton.addTarget(Main.self, action: nAction, for: .touchUpInside)
        self.layoutIfNeeded()
        self.layoutSubviews()
    }
}
}

EnablePopOuts runs but the northButton doesn't appear.

Upvotes: 1

Views: 138

Answers (0)

Related Questions