Search code examples
iosswiftmaterial-componentsmaterial-components-ios

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?


Solution

  • 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)