Answers for "uibutton swift"

1

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")
          }
Posted by: Guest on July-05-2020
0

swiftui button

//A button in swiftui uses the closure syntax

import SwiftUI

Button(action: {
  //This part of the button is basically what the button does, it's "action"
}, label: {
  //This part of the button is what the button will look like, the placeholder
}
       
//Therefore, one of the examples of this include:
       
Button(action: {
	number += 1
}, label: {
  	Text("Increase number")
  		.foregroundColor(.red)
  	//Makes the font colour of the text red
})
       
//In here, the button's placeholder will be the text "Increase Number", and it's action is that it increments the number
//It is also possible to add modifiers to the text, since text is a view, such as the .foregroundColor modifier
//There are two ways to simplify this Button's syntax
       
Buttton { 
  	number += 1
} label: {
	Text("Increase number")
  		.foregroundColor(.red)
}
       
//This allows us to remove the action:, the paranthesis, and the comma.
//There is one more stage of simplification
       
Button(placeholder -> Text) {
  	//action
}

//This simplifed syntax does the exact same thing, but the placeholder cannot be a view but rather a string.
//This syntax can be used if you dont need to modify the placeholder text.
Posted by: Guest on August-13-2021
0

button swift ui

# Definition of A Button
# A control that initiates an action

Declaration
struct Button<Label> where Label : View
Posted by: Guest on October-21-2021

Code answers related to "Swift"

Browse Popular Code Answers by Language