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

button click programmatically swift

button.sendActions(for: .touchUpInside)
Comment

PREVIOUS NEXT
Code Example
Swift :: Swift Switch Statement with Range 
Swift :: add callbacks to view swiftui 
Swift :: swift paged scrollview get current page 
Swift :: swiftui radio button 
Swift :: uilabel set font 
Swift :: hstack spacing swiftui 
Swift :: swift extension Array of element 
Swift :: Function Inside Swift Struct 
Swift :: Methods Swift 
Swift :: while loop in swift 
Swift :: swiftui line break text 
Swift :: two value sum in swift 
Swift :: Swift Expressions 
Swift :: printf in swift 
Swift :: Swift Change Value of a Variable 
Swift :: Swift Join Two Strings 
Swift :: Swift Named Tuple 
Swift :: SwiftUI cant tap in spacer of HStack 
Swift :: Create a Throwing Function Swift 
Swift :: Swift break and continue Inside Nested Loop 
Swift :: Declare Variables in Swift 
Swift :: Swift enum Associated Values 
Swift :: How to Hide Password in Text field Swift 
Swift :: Swift Conforming Multiple Protocols 
Swift :: vibrations in ios swift 
Ruby :: rails kill server 
Ruby :: embedded ruby tag 
Ruby :: How can I rename a database column in a Ruby on Rails migration? 
Ruby :: ruby on rails rollback migration 
Ruby :: rails g model 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =