Alex Kirov
Alex Kirov

Reputation: 49

How to add Material Design Button in iOS swift app?

I have read the manual (https://material.io/develop/ios/components/buttons/), but still have not understood how to do it.

class FloatingButtonController: UIViewController {

override func viewDidLoad() {
    super.viewDidLoad()

    let floatingButton = MDCFloatingButton()
    floatingButton.setImage( UIImage(named: "plus"), for: .normal)
    floatingButton.backgroundColor = .white
    floatingButton.setElevation(ShadowElevation(rawValue: 6), for: .normal)
    floatingButton.addTarget(self, action: #selector(btnFloatingButtonTapped(floatingButton:)), for: .touchUpInside)
    self.view.addSubview(floatingButton)
}

@objc func btnFloatingButtonTapped(floatingButton: MDCFloatingButton){
    floatingButton.collapse(true) {
        floatingButton.expand(true, completion: nil)
    }
}
}

My project on the screen. As you can see - the button is missing on the screen. When you click on a blank screen errors appear.

Button touch target does not meet minimum size guidlines of (48, 48). Button: >, Touch Target: {0, 0}

Screenshot of my project

Tell me, please, what am I doing wrong? And how do I get the job right?

Upvotes: 0

Views: 3458

Answers (2)

Lal Krishna
Lal Krishna

Reputation: 16160

To add button target the button should have size atlease 48x48.

You could constraint it manually or set frame.

OR you could add button via Storyboard too.

Upvotes: 0

Mahendra
Mahendra

Reputation: 8914

You didn't set the frame to button. You need to specify (x, y) position to place button in view.

floatingButton.frame = CGRect(x: 300, y: 300, width: 48, height: 48)

Upvotes: 2

Related Questions