0

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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sayooj
  • 375
  • 3
  • 13
  • What is `view` in `view.layoutMarginsGuide.rightAnchor`? – trungduc Jun 08 '18 at 11:31
  • your code looks great, it should work, what about safearea? try running it in iPhone 6s – PPL Jun 08 '18 at 11:39
  • Check the view hierarchy in the UI debugger if there is any other view over UIButton. – PGDev Jun 08 '18 at 11:43
  • Where does the button get added to the view hierarchy? – vacawama Jun 08 '18 at 11:44
  • @trungduc view is just self . Ive updated the code . I've removed all constrains and just use frame to view the button ,everything works except the the addTarget – Sayooj Jun 08 '18 at 12:08
  • @PPL I've tried with just frame instead of constrains still didnt work – Sayooj Jun 08 '18 at 12:09
  • @vacawama From the init() i've update the code please check – Sayooj Jun 08 '18 at 12:10
  • Your button has a size of 60x60, but its superview has 0 width and 0 height because you didn't specify a frame when you created it with `let floatingButton = FloatingView()`. – vacawama Jun 08 '18 at 12:21
  • @vacawama i've specified few constrains for floatingView in view controller `floatingButton.rightAnchor.constraint(equalTo: view.layoutMarginsGuide.rightAnchor ).isActive = true floatingButton.bottomAnchor.constraint(equalTo: view.layoutMarginsGuide.bottomAnchor).isActive = true floatingButton.widthAnchor.constraint(equalToConstant: 150).isActive = true floatingButton.heightAnchor.constraint(equalTo: floatingButton.widthAnchor).isActive = true` – Sayooj Jun 08 '18 at 12:27
  • @sayooj - your code works fine for me as-is... Is it possible you don't have the Console open in the Debug Pane, so you're just not seeing the `print("Clicked..")` output? – DonMag Jun 08 '18 at 13:06
  • @DonMag No consoles . It works when I add the same button and target function directly to view controller instead of the custom view class . – Sayooj Jun 08 '18 at 13:14
  • @DonMag Ive updated the code . Please let me know whether i'm doing right or not . – Sayooj Jun 08 '18 at 13:31
  • 1
    @sayooj - I put up a simple project on GitHub with your code, and the button tap results in "Clicked.." being output to the debug console. Take a look - see if it works for you: https://github.com/DonMag/SayoojFLoatingButton – DonMag Jun 08 '18 at 13:55
  • Thanks a lot @DonMag . This project works . I'll try to update mine accordingly . – Sayooj Jun 08 '18 at 14:52

2 Answers2

1

try to change the action to

action: #selector(buttonClicked(_:))

and the function to

@objc func buttonClicked(_ sender: UIButton){..}

Vasilis D.
  • 1,416
  • 13
  • 21
0

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

    }
}
Sayooj
  • 375
  • 3
  • 13