Crie um UIButton programaticamente no Swift

130

Estou tentando criar UI's programaticamente. Como faço para que essa ação funcione? Estou desenvolvendo com Swift.

Código em viewDidLoad:

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        let myFirstLabel = UILabel()
        let myFirstButton = UIButton()
        myFirstLabel.text = "I made a label on the screen #toogood4you"
        myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
        myFirstLabel.textColor = UIColor.redColor()
        myFirstLabel.textAlignment = .Center
        myFirstLabel.numberOfLines = 5
        myFirstLabel.frame = CGRectMake(15, 54, 300, 500)
        myFirstButton.setTitle("✸", forState: .Normal)
        myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
        myFirstButton.frame = CGRectMake(15, -50, 300, 500)
        myFirstButton.addTarget(self, action: "pressed", forControlEvents: .TouchUpInside)
        self.view.addSubview(myFirstLabel)
        self.view.addSubview(myFirstButton)
    }

        func pressed(sender: UIButton!) {
            var alertView = UIAlertView();
            alertView.addButtonWithTitle("Ok");
            alertView.title = "title";
            alertView.message = "message";
            alertView.show();
        }
Benr783
fonte

Respostas:

202

Você está faltando apenas dois pontos no final do nome do seletor. Como pressionado usa um parâmetro, os dois pontos devem estar lá. Além disso, sua função pressionada não deve ser aninhada dentro do viewDidLoad.

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let myFirstLabel = UILabel()
    let myFirstButton = UIButton()
    myFirstLabel.text = "I made a label on the screen #toogood4you"
    myFirstLabel.font = UIFont(name: "MarkerFelt-Thin", size: 45)
    myFirstLabel.textColor = UIColor.redColor()
    myFirstLabel.textAlignment = .Center
    myFirstLabel.numberOfLines = 5
    myFirstLabel.frame = CGRectMake(15, 54, 300, 500)
    myFirstButton.setTitle("✸", forState: .Normal)
    myFirstButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
    myFirstButton.frame = CGRectMake(15, -50, 300, 500)
    myFirstButton.addTarget(self, action: #selector(myClass.pressed(_:)), forControlEvents: .TouchUpInside)
    self.view.addSubview(myFirstLabel)
    self.view.addSubview(myFirstButton)
}

@objc func pressed(sender: UIButton!) {
    var alertView = UIAlertView()
    alertView.addButtonWithTitle("Ok")
    alertView.title = "title"
    alertView.message = "message"
    alertView.show()
}

EDIT: atualizado para refletir as melhores práticas no Swift 2.2. #selector () deve ser usado em vez de uma string literal que está obsoleta.

Traço
fonte
O aplicativo ainda trava quando pressiono o botão.
precisa saber é o seguinte
Desculpe corrigido. A função não deve ser aninhada.
Dash
1
Ei, você pode me dizer a lógica por trás dos dois pontos que precisamos adicionar após a string que representa a ação?
juniorgarcia
2
@chorajunior Os dois pontos são necessários porque a pressedfunção aceita um argumento.
HughHughTeotl
Inversamente, qualquer pessoa que esteja lendo isso também pode querer saber que, se a ação do seu botão referenciar uma função que não usa parâmetro, os dois pontos não são necessários e podem até causar um erro se não forem removidos.
Dave G
60

Swift 2.2 Xcode 7.3

Como os literais de cadeia de caracteres Objective-C estão obsoletos agora para métodos de retorno de chamada de botão

let button:UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
button.backgroundColor = UIColor.blackColor()
button.setTitle("Button", forState: UIControlState.Normal)
button.addTarget(self, action:#selector(self.buttonClicked), forControlEvents: .TouchUpInside)
self.view.addSubview(button)

func buttonClicked() {
     print("Button Clicked")
}

Swift 3 Xcode 8

let button:UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
button.backgroundColor = .black
button.setTitle("Button", for: .normal)
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)

func buttonClicked() {
    print("Button Clicked")
}

Swift 4 Xcode 9

let button:UIButton = UIButton(frame: CGRect(x: 100, y: 400, width: 100, height: 50))
button.backgroundColor = .black
button.setTitle("Button", for: .normal)
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)

@objc func buttonClicked() {
    print("Button Clicked")
}
n.by.n
fonte
2
@ n.by.n como passar argumento sobre este método buttonClicked ()?
ArgaPK
20

Swift 4/5

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
            }
JP Aquino
fonte
15

Swift 4

    private func createButton {
        let sayButtonT = UIButton(type: .custom)
        sayButtonT.addTarget(self, action: #selector(sayAction(_:)), for: .touchUpInside)
    }

    @objc private func sayAction(_ sender: UIButton?) {

    }
Raman
fonte
12

Sim no simulador. Algumas vezes, ele não reconhece o seletor, parece que há um erro. Mesmo que eu não tenha enfrentado o seu código, alterei o nome da ação (seletor). Funciona

let buttonPuzzle:UIButton = UIButton(frame: CGRectMake(100, 400, 100, 50))
buttonPuzzle.backgroundColor = UIColor.greenColor()
buttonPuzzle.setTitle("Puzzle", forState: UIControlState.Normal)
buttonPuzzle.addTarget(self, action: "buttonAction:", forControlEvents: UIControlEvents.TouchUpInside)
buttonPuzzle.tag = 22;
self.view.addSubview(buttonPuzzle)

Um exemplo de função seletora está aqui:

func buttonAction(sender:UIButton!) {
    var btnsendtag:UIButton = sender
    if btnsendtag.tag == 22 {            
        //println("Button tapped tag 22")
    }
}
Comunidade
fonte
8

Você deve ser capaz de criar um botão da personalização da interface do usuário por meio de programação, acessando o titleLabel propriedade de UIButton .

Por referência de classe no Swift: em relação à propriedade titleLabel , ele diz que "embora essa propriedade seja somente leitura, suas próprias propriedades são de leitura / gravação. Use essas propriedades principalmente para configurar o texto do botão".

No Swift , você pode modificar diretamente as propriedades do titleLabel da seguinte maneira:

let myFirstButton = UIButton()
myFirstButton.titleLabel!.text = "I made a label on the screen #toogood4you"
myFirstButton.titleLabel!.font = UIFont(name: "MarkerFelt-Thin", size: 45)
myFirstButton.titleLabel!.textColor = UIColor.red
myFirstButton.titleLabel!.textAlignment = .center
myFirstButton.titleLabel!.numberOfLines = 5
myFirstButton.titleLabel!.frame = CGRect(x: 15, y: 54, width: 300, height: 500)

Editar

Sintaxe do Swift 3.1

samatron
fonte
1
Eu tentei executar este código e ele não está funcionando para mim. Non dos chidlren de titleLabel parecem existir
user2202911
7

tente estes ... espero que ajude ...

override func viewDidLoad() {
super.viewDidLoad()  

    let btn = UIButton()
    btn.frame = CGRectMake(10, 10, 50, 50)  //set frame
    btn.setTitle("btn", forState: .Normal)  //set button title
    btn.setTitleColor(UIColor.redColor(), forState: .Normal) //set button title color
    btn.backgroundColor = UIColor.greenColor() //set button background color
    btn.tag = 1 // set button tag
    btn.addTarget(self, action: "btnclicked:", forControlEvents: .TouchUpInside) //add button action
    self.view.addSubview(btn) //add button in view

}

estes são botões, clique em evento.

func btnclicked(sender: UIButton!) 
{
    //write the task you want to perform on buttons click event..
}
krushnsinh
fonte
3

Swift 3: você pode criar um UIButtonprogramaticamente

dentro de um escopo de métodos, por exemplo, em ViewDidLoad() Certifique-se de adicionar restrições ao botão, caso contrário você não o verá

let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.target(forAction: #selector(buttonAction), withSender: self)
//button.backgroundColor etc

view.addSubview(button)

func buttonAction() {
   //some Action
}

ou fora do seu escopo como variável global para acessá-lo de qualquer lugar do seu module

let button: UIButton = {
   let b = UIButton()
   b.translatesAutoresizingMaskIntoConstraints = false
   //b.backgroundColor etc
   return b
}()

e então você configura as restrições

func setupButtonView() {
   view.addSubview(button)
   button.widthAnchor.constraint(equalToConstant: 40).isActive = true
   button.heightAnchor.constraint(equalToConstant: 40).isActive = true
   // etc

}
phitsch
fonte
2

Swift: o botão Ui cria programaticamente,

var button: UIButton = UIButton(type: .Custom)

button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0)

button.addTarget(self, action: #selector(self.aMethod), forControlEvents: .TouchUpInside)

button.tag=2

button.setTitle("Hallo World", forState: .Normal)

view.addSubview(button)


func aMethod(sender: AnyObject) {
    print("you clicked on button \(sender.tag)")
}
Ananthu R Krishnan
fonte
2

Extensão Swift "Button factory" para UIButton (e enquanto estamos nisso) também para UILabel da seguinte forma:

extension UILabel
{
// A simple UILabel factory function
// returns instance of itself configured with the given parameters

// use example (in a UIView or any other class that inherits from UIView):

//   addSubview(   UILabel().make(     x: 0, y: 0, w: 100, h: 30,
//                                   txt: "Hello World!",
//                                 align: .center,
//                                   fnt: aUIFont,
//                              fntColor: UIColor.red)                 )
//

func make(x: CGFloat, y: CGFloat, w: CGFloat, h: CGFloat,
          txt: String,
          align: NSTextAlignment,
          fnt: UIFont,
          fntColor: UIColor)-> UILabel
{
    frame = CGRect(x: x, y: y, width: w, height: h)
    adjustsFontSizeToFitWidth = true
    textAlignment = align
    text = txt
    textColor = fntColor
    font = fnt
    return self
}
// Of course, you can make more advanced factory functions etc.
// Also one could subclass UILabel, but this seems to be a     convenient case for an extension.
}


extension UIButton
{
// UIButton factory returns instance of UIButton
//usage example:

// addSubview(UIButton().make(x: btnx, y:100, w: btnw, h: btnh,
// title: "play", backColor: .red,
// target: self,
// touchDown: #selector(play), touchUp: #selector(stopPlay)))


func make(   x: CGFloat,y: CGFloat,
             w: CGFloat,h: CGFloat,
                  title: String, backColor: UIColor,
                  target: UIView,
                  touchDown:  Selector,
                  touchUp:    Selector ) -> UIButton
{
    frame = CGRect(x: x, y: y, width: w, height: h)
    backgroundColor = backColor
    setTitle(title, for: .normal)
    addTarget(target, action: touchDown, for: .touchDown)
    addTarget(target, action: touchUp  , for: .touchUpInside)
    addTarget(target, action: touchUp  , for: .touchUpOutside)

    return self
}
}

Testado no Swift no Xcode Versão 9.2 (9C40b) Swift 4.x

Ted van Gaalen
fonte
1

Swift: o botão Ui cria programaticamente

let myButton = UIButton() 
myButton.titleLabel!.frame = CGRectMake(15, 54, 300, 500) 
myButton.titleLabel!.text = "Button Label"
myButton.titleLabel!.textColor = UIColor.redColor()
myButton.titleLabel!.textAlignment = .Center
Shanmugasundharam
fonte
1

Para Swift 3 Xcode 8 .......

let button = UIButton(frame: CGRect(x: 0, y: 0, width: container.width, height: container.height))
        button.addTarget(self, action: #selector(self.barItemTapped), for: .touchUpInside)


func barItemTapped(sender : UIButton) {
    //Write button action here
}
emraz
fonte
0

UIButton com restrições em iOS 9.1/Xcode 7.1.1/Swift 2.1:

import UIKit
import MapKit

class MapViewController: UIViewController {  

    override func loadView() {
        mapView = MKMapView()  //Create a view...
        view = mapView         //assign it to the ViewController's (inherited) view property.
                               //Equivalent to self.view = mapView

        myButton = UIButton(type: .RoundedRect)  //RoundedRect is an alias for System (tested by printing out their rawValue's)
        //myButton.frame = CGRect(x:50, y:500, width:70, height:50)  //Doesn't seem to be necessary when using constraints.
        myButton.setTitle("Current\nLocation", forState: .Normal)
        myButton.titleLabel?.lineBreakMode = .ByWordWrapping  //If newline in title, split title onto multiple lines
        myButton.titleLabel?.textAlignment = .Center
        myButton.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        myButton.layer.cornerRadius = 6   //For some reason, a button with type RoundedRect has square corners
        myButton.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.5) //Make the color partially transparent
        //Attempt to add padding around text. Shrunk the frame when I tried it.  Negative values had no effect.
        //myButton.titleEdgeInsets = UIEdgeInsetsMake(-10,-10,-10,-10)
        myButton.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5)  //Add padding around text.

        myButton.addTarget(self, action: "getCurrentLocation:", forControlEvents: .TouchUpInside)
        mapView.addSubview(myButton)

        //Button Constraints:
        myButton.translatesAutoresizingMaskIntoConstraints = false //***
        //bottomLayoutGuide(for tab bar) and topLayoutGuide(for status bar) are properties of the ViewController
        //To anchor above the tab bar on the bottom of the screen:
        let bottomButtonConstraint = myButton.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -20) //Implied call of self.bottomLayoutGuide. Anchor 20 points **above** the top of the tab bar.
        //To anchor to the blue guide line that is inset from the left 
        //edge of the screen in InterfaceBuilder:
        let margins = view.layoutMarginsGuide  //Now the guide is a property of the View.
        let leadingButtonConstraint = myButton.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor)

        bottomButtonConstraint.active = true
        leadingButtonConstraint.active = true
    }


    func getCurrentLocation(sender: UIButton) {
        print("Current Location button clicked!")
    }

O botão está ancorado no canto inferior esquerdo, acima da barra de guias.

7stud
fonte
como getCurrentLocationnos ajuda a obter a localização, por favor?
Nyxee 23/05
@nyxee, A questão aqui era sobre como criar um botão programaticamente. Obter a localização de um usuário não tem nada a ver com a criação de um botão. Para obter a localização de um usuário, veja aqui; developer.apple.com/reference/corelocation/cllocationmanager . Se você não conseguir descobrir, faça sua própria pergunta, por favor.
7stud
0

Em Swift, podemos criar um botão programaticamente, escrevendo esse código em nosso arquivo viewcontroller.swift ...

import UIKit

class ViewController: UIViewController
{  
private let firstbutton:UIButton = UIButton()

override func viewDidLoad() {
    super.viewDidLoad()
    self.firstbutton = UIButton.buttonWithType(UIButtonType.Custom) as? UIButton
    self.firstbutton!.frame = CGRectMake(100, 200, 100, 100)
    self.firstbutton!.backgroundColor = UIColor.redColor()
    self.firstbutton!.setTitle("My Button", forState: UIControlState.Normal)
    self.firstbutton!.addTarget(self, action:#selector(self.firstButtonClicked), forControlEvents: .TouchUpInside)
    self.view.addSubview(firstbutton!)
    }

func firstButtonClicked(){
   print("First Button Clicked")
}
onCompletion
fonte
Sem o @objc for selector, ele não está aceitando.
Pradumna Patil 9/10/19
0

Usando isso no Objective-C

UIButton *testButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[testButton setTitle:@"Go to here" forState:UIControlStateNormal];
testButton.frame = CGRectMake(20, 20, 150, 150);    
[self.view addSubview:testButton];

Usando isso no mais recente Swift

let testButton   = UIButton(type: UIButtonType.system) as UIButton
testButton.frame = CGRectMake(160, 160, 80, 20)
testButton.backgroundColor = UIColor.green
testButton.setTitle("Button testing:- ", forState: UIControlState.normal)
self.view.addSubview(testButton)
Rex
fonte
0

Se você for para a parte principal do storyboard, e no canto inferior direito, vá para o círculo com um quadrado e use um botão em branco. Em seguida, no código, use @IBAction com ele para conectá-lo. Em seguida, você pode criar uma função @IBAction com ele.

Tyler
fonte
0

Swift 4.2 - XCode 10.1

Usando um fechamento

let button: UIButton = {
  let button = UIButton(type: .system)
  button.titleLabel?.font = UIFont.systemFont(ofSize: 20)
  ...
  return button
}()
Saranjith
fonte
0

No iOS 12, Swift 4.2 e XCode 10.1

//For system type button
let button = UIButton(type: .system)
button.frame = CGRect(x: 100, y: 250, width: 100, height: 50)
//        button.backgroundColor = .blue
button.setTitle("Button", for: .normal)
button.setTitleColor(.white, for: .normal)
button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 13.0)
button.titleLabel?.textAlignment = .center//Text alighment center
button.titleLabel?.numberOfLines = 0//To display multiple lines in UIButton
button.titleLabel?.lineBreakMode = .byWordWrapping//By word wrapping
button.tag = 1//To assign tag value
button.btnProperties()//Call UIButton properties from extension function
button.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button)

//For custom type button (add image to your button)
let button2 = UIButton(type: .custom)
button2.frame = CGRect(x: 100, y: 400, width: 100, height: 50)
//        button2.backgroundColor = .blue
button2.setImage(UIImage.init(named: "img.png"), for: .normal)
button2.tag = 2
button2.btnProperties()//Call UIButton properties from extension function
button2.addTarget(self, action:#selector(self.buttonClicked), for: .touchUpInside)
self.view.addSubview(button2)

@objc func buttonClicked(sender:UIButton) {
    print("Button \(sender.tag) clicked")
}

//You can add UIButton properties like this also
extension UIButton {
    func btnProperties() {
        layer.cornerRadius = 10//Set button corner radious
        clipsToBounds = true
        backgroundColor = .blue//Set background colour
        //titleLabel?.textAlignment = .center//add properties like this
    }
}
iOS
fonte
0

Swift 5.0

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

insira a descrição da imagem aqui

Gurjinder Singh
fonte