Swift Create Custom Button Programmatically

[Solved] Swift Create Custom Button Programmatically | Swift - Code Explorer | yomemimo.com
Question : set button programmatically swift

Answered by : motionless-meerkat-2yf7svpcgqu1

 let button = self.makeButton(title: "Login", titleColor: .blue, font: UIFont.init(name: "Arial", size: 18.0), background: .white, cornerRadius: 3.0, borderWidth: 2, borderColor: .black) view.addSubview(button) // Adding Constraints button.heightAnchor.constraint(equalToConstant: 40).isActive = true button.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 40).isActive = true button.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -40).isActive = true button.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: -400).isActive = true button.addTarget(self, action: #selector(pressed(_ :)), for: .touchUpInside) // Define commmon method func makeButton(title: String? = nil, titleColor: UIColor = .black, font: UIFont? = nil, background: UIColor = .clear, cornerRadius: CGFloat = 0, borderWidth: CGFloat = 0, borderColor: UIColor = .clear) -> UIButton { let button = UIButton() button.translatesAutoresizingMaskIntoConstraints = false button.setTitle(title, for: .normal) button.backgroundColor = background button.setTitleColor(titleColor, for: .normal) button.titleLabel?.font = font button.layer.cornerRadius = 6.0 button.layer.borderWidth = 2 button.layer.borderColor = UIColor.red.cgColor return button } // Button Action @objc func pressed(_ sender: UIButton) { print("Pressed") }

Source : https://stackoverflow.com/questions/24102191/make-a-uibutton-programmatically-in-swift | Last Update : Sun, 12 Jul 20

Question : swift create custom button programmatically

Answered by : enthusiastic-echidna-5spn2dzsop8z

let button = UIButton(frame: CGRect(x: 20, y: 20, width: 200, height: 60)) button.setTitle("Email", for: .normal) button.backgroundColor = .white button.setTitleColor(UIColor.black, for: .normal) button.addTarget(self, action: #selector(self.buttonTapped), for: .touchUpInside) myView.addSubview(button)
@objc func buttonTapped(sender : UIButton) { //Write button action here }

Source : https://stackoverflow.com/questions/24102191/make-a-uibutton-programmatically-in-swift | Last Update : Thu, 10 Jun 21

Answers related to swift create custom button programmatically

Code Explorer Popular Question For Swift