Search
 
SCRIPT & CODE EXAMPLE
 

SWIFT

set button programmatically swift

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

swift create custom button programmatically

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
            }
Comment

button click programmatically swift

button.sendActions(for: .touchUpInside)
Comment

PREVIOUS NEXT
Code Example
Swift :: push view controller programmatically swift 5 
Swift :: swift 5 get current date date 
Swift :: difference between struct and class swift 
Swift :: How to find index of list item in Swift? 
Swift :: We use the for loop to iterate over the elements of a dictionary. 
Swift :: swift hello world 
Swift :: power swift 
Swift :: add navigation bar button swiftui 
Swift :: PDF Preview in Swift iOS 
Swift :: how to add an underline to a textField swift 
Swift :: swft ui image 
Swift :: swift pdf preview image 
Swift :: Swift Remove an Element from a Dictionary 
Swift :: uilabel set fon siz 
Swift :: swiftui radio button 
Swift :: How to convert String into Array of character 
Swift :: swiftui text editor 
Swift :: swift add enum storyboard 
Swift :: two value sum in swift 
Swift :: swift go to main thread 
Swift :: swift how to wait in a loop 
Swift :: Swift Autoclosure 
Swift :: swift conditional statements 
Swift :: swift for loop with where caluse 
Swift :: continue keyword in swift language 
Swift :: Allow user to import image 
Swift :: how to use snippets in xcode 
Swift :: swift writing to ios logs 
Swift :: password reset with phone number in firebase flutter 
Ruby :: how to match email in regex in ruby 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =