iosswiftuibuttonaddtarget

addTarget on UIButton not working


I have a UIView class

class FloatingView  : UIView {
    lazy var floatingButton : UIButton = {
        let button = UIButton(type: UIButtonType.system)
        button.setBackgroundImage(#imageLiteral(resourceName: "ic_add_circle_white_36pt"), for: .normal)
        button.tintColor = UIColor().themePurple()
        button.addTarget(self, action: #selector(buttonClicked), for: UIControlEvents.touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupViews()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupViews(){ addSubview(floatingButton) }

    override func didMoveToWindow() {
        super.didMoveToWindow()

        floatingButton.rightAnchor.constraint(equalTo: layoutMarginsGuide.rightAnchor).isActive = true
        floatingButton.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor).isActive = true
        floatingButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
        floatingButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    }

    @objc func buttonClicked (){
        print("Clicked..")
    }

Added this to view by

let floatingButton = FloatingView()
view.addSubview(floatingButton)

I've also specified the constraints for the floating view .

Screenshot of button

The button got added to view as expected but the "buttonClicked" function not invoked when the button is clicked . The fade animation on the button when clicked is working though.I've tried UITapGesture but not working .

I've update the class as below

class FloatingView{
    lazy var floatingButton : UIButton = {
        let button = UIButton(type: UIButtonType.system)
        button.setBackgroundImage(#imageLiteral(resourceName: "ic_add_circle_white_36pt"), for: .normal)
        button.tintColor = UIColor().themePurple()
        button.addTarget(self, action: #selector(buttonClicked(_:)), for: UIControlEvents.touchUpInside)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    private var view : UIView!

    func add(onview: UIView ){
        view = onview
        configureSubViews()
    }

    private func configureSubViews(){
        view.addSubview(floatingButton)
        floatingButton.rightAnchor.constraint(equalTo: view.layoutMarginsGuide.rightAnchor).isActive = true
        floatingButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor).isActive = true
        floatingButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
        floatingButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
    }

    @objc func buttonClicked(_ sender : UIButton){
        print("Button Clicked")
    }
}

And in controller

let flButton = FloatingView()
flButton.add(onview: view)

I'm trying to create a floating action button like this. I'm not sure whether I'm doing it the right way.


Solution

  • I somehow fixed it by setting the constrains from within the custom class to its superview rather than from the controller.

    func configureSubviews(){
        if let superView = superview {
            superView.addSubview(floatingButton)
            widthAnchor.constraint(equalToConstant: circleSpanArea).isActive = true
            heightAnchor.constraint(equalTo: widthAnchor).isActive = true
            centerXAnchor.constraint(equalTo: floatingButton.centerXAnchor).isActive = true
            centerYAnchor.constraint(equalTo: floatingButton.centerYAnchor).isActive = true
    
            floatingButton.rightAnchor.constraint(equalTo: superView.layoutMarginsGuide.rightAnchor).isActive = true
            floatingButton.bottomAnchor.constraint(equalTo: superView.layoutMarginsGuide.bottomAnchor).isActive = true
            floatingButton.heightAnchor.constraint(equalToConstant: 60).isActive = true
            floatingButton.widthAnchor.constraint(equalToConstant: 60).isActive = true
    
        }
    }