swift create custom button programmatically

        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")
          }

3.89
9
Robin Manoli 100 points

                                    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
            }

3.89 (9 Votes)
0
Are there any code examples left?
Create a Free Account
Unlock the power of data and AI by diving into Python, ChatGPT, SQL, Power BI, and beyond.
Sign up
Develop soft skills on BrainApps
Complete the IQ Test
Relative searches
swift add button programmatically to view press button programmatically swift change button type programmatically swift custom button swift ui swift how to set button to custom swift custom button custom button class swift swiftUI how to create custom button swiftui programmatically click the buton add button to view programmatically swift click a button programmatically in swift custom uibutton class swift programmatically add button swift programmatically how to custom Button component in swift swift custom button framework swift create custom button programmatically swift button click programmatically button programmatically swift button program in swift uibutton in swift programmatically add button action programmatically swift swift create button programmatically create button programmatically swift swift add button programmatically how to programmatically set button state ios swift add button programmatically swift swift uibutton programmatically button constrant programatically swift create button programmatically swift 4 how to create button in swift programmatically make ui buttons programmatically in swift how to programmatically click button in swift swift create a button programmatically programmatically add action to a uibutton in swift view programmatically swift button callback swift add + button swiftui custom button swift declare a uibutton return uibutton function ios swift 5 swift programmatically add button uibutton swift create uibutton programmatically swift how to create uiibutton programmatically in swift wuth function how to create a uibutton programmatically in swift how to add a button programmatically in swift UIButton swift click action add action to UIButton swift how to create a button programmatically in swift 4 set button response swift add uibutton programmatically swift add UIBButton action inswift create uibutton proramatically closure uibutton programmatically swift custom uibutton swift programmatically uibutton tutorial programmatically programmatically add functions to uibutton swift swift create uibutton programmatically add button action swift prepare button swift add contact uibutton swift button action in swift cell.btnShowMore.addTarget(self, action: #selector(showMoreAtn(sender: UIButton(), label: cell.lblPackageDecription), for: .touchUpInside) in swift4 ios uibutton programmatically selector change screens method ios uibutton programmatically ios uibutton how to set action in swift button swift programmatically add button ontap add ASAuthorizationAppleIDButt to button in swift UIButton uibutton swift onClick how to add a action to a UIButton swift programmatically button swift swift and how to press button programatically swift uibutton add target create ui button connection programmatically swift make a uiBUTTON when click on it swift 5
Made with love
This website uses cookies to make IQCode work for you. By using this site, you agree to our cookie policy

Welcome Back!

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign in
Recover lost password
Or log in with

Create a Free Account

Sign up to unlock all of IQCode features:
  • Test your skills and track progress
  • Engage in comprehensive interactive courses
  • Commit to daily skill-enhancing challenges
  • Solve practical, real-world issues
  • Share your insights and learnings
Create an account
Sign up
Or sign up with
By signing up, you agree to the Terms and Conditions and Privacy Policy. You also agree to receive product-related marketing emails from IQCode, which you can unsubscribe from at any time.
Creating a new code example
Code snippet title
Source