如何以编程方式创建按钮?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何以编程方式创建按钮?相关的知识,希望对你有一定的参考价值。

如何在Swift中以编程方式创建图形元素(如UIButton)?我试图在视图中创建并添加按钮,但无法进行。

答案

以下是使用targetAction以编程方式添加UIButton的完整解决方案。 Swift 2.2

override func viewDidLoad() 
  super.viewDidLoad()

  let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
  button.backgroundColor = .greenColor()
  button.setTitle("Test Button", forState: .Normal)
  button.addTarget(self, action: #selector(buttonAction), forControlEvents: .TouchUpInside)

  self.view.addSubview(button)


func buttonAction(sender: UIButton!) 
  print("Button tapped")

使用NSLayoutConstraint而不是frame来正确放置每个iPhone屏幕的按钮可能更好。

更新了Swift 3.1的代码:

override func viewDidLoad() 
  super.viewDidLoad()

  let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
  button.backgroundColor = .green
  button.setTitle("Test Button", for: .normal)
  button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

  self.view.addSubview(button)


func buttonAction(sender: UIButton!) 
  print("Button tapped")

更新了Swift 4.2的代码:

override func viewDidLoad() 
  super.viewDidLoad()

  let button = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
  button.backgroundColor = .green
  button.setTitle("Test Button", for: .normal)
  button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)

  self.view.addSubview(button)


@objc func buttonAction(sender: UIButton!) 
  print("Button tapped")

如果func buttonAction被宣布为privateinternal,上述仍然有效。

另一答案

您可以像这样创建,也可以像这样添加动作....

import UIKit

let myButton = UIButton(frame: CGRect(x: 0, y: 0, width: 50, height: 50))

init(nibName nibNameOrNil: String!, bundle nibBundleOrNil: NSBundle!)
       super.init(nibName: nibName, bundle: nibBundle) 
        myButton.targetForAction("tappedButton:", withSender: self)


func tappedButton(sender: UIButton!)
 
     println("tapped button")

另一答案

有可能的。除了使用swift语法之外,你几乎以同样的方式做所有事情。例如,你可以在这样的代码中创建一个UIButton:

 var button: UIButton = UIButton(frame: CGRectMake(0, 0, 100, 100))
另一答案

从故事板创建UIButton:1 - 将UIButton对象从对象库拖到故事板文件2中的ViewController - 显示助手编辑器3 - 从UIButton创建上方右键拖动到您的类中。结果如下:

@IBAction func buttonActionFromStoryboard(sender: UIButton)

    println("Button Action From Storyboard")

要以编程方式创建UIButton:1-写入“override func viewDidLoad()”:

        let uiButton    = UIButton.buttonWithType(UIButtonType.System) as UIButton
        uiButton.frame  = CGRectMake(16, 116, 288, 30)
        uiButton.setTitle("Second", forState: UIControlState.Normal);
        uiButton.addTarget(self, action: "buttonActionFromCode:", forControlEvents: UIControlEvents.TouchUpInside)
        self.view.addSubview(uiButton)

2-添加IBAction函数:

@IBAction func buttonActionFromCode(sender:UIButton)

    println("Button Action From Code")

另一答案
            let myFirstButton = UIButton()
            myFirstButton.setTitle("Software Button", forState: .Normal)
            myFirstButton.setTitleColor(UIColor.redColor(), forState: .Normal)
            myFirstButton.frame = CGRectMake(100, 300, 150, 50)
            myFirstButton.backgroundColor = UIColor.purpleColor()
            myFirstButton.layer.cornerRadius = 14
            myFirstButton.addTarget(self, action: "pressed:", forControlEvents: .TouchUpInside)
            self.view.addSubview(myFirstButton)
            myFirstButton.hidden=true
            nameText.delegate = self


func pressed(sender: UIButton!) 
        var alertView = UIAlertView()
        alertView.addButtonWithTitle("Ok")
        alertView.title = "title"
        alertView.message = "message"
        alertView.show();
    
另一答案

是的在模拟器中。有时它不会识别选择器,看起来有一个错误。即使我不面对你的代码,我只是改变了动作名称(选择器)。有用

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)

选择器功能在这里:

func buttonAction(sender:UIButton!)


    var btnsendtag:UIButton = sender
    if btnsendtag.tag == 22             
        //println("Button tapped tag 22")
    

另一答案

这对我很有用,#DynamicButtonEvent #ios #Swift #Xcode

func setupButtonMap()
    let mapButton = UIButton(type: .system)
    mapButton.setImage(#imageLiteral(resourceName: "CreateTrip").withRenderingMode(.alwaysOriginal), for: .normal)
    mapButton.frame = CGRect(x: 0, y: 0, width: 34, height: 34)
    mapButton.contentMode = .scaleAspectFit
    mapButton.backgroundColor = UIColor.clear
    mapButton.addTarget(self, action: #selector(ViewController.btnOpenMap(_:)), for: .touchUpInside)
    navigationItem.leftBarButtonItem = UIBarButtonItem(customView: mapButton)
    
@IBAction func btnOpenMap(_ sender: Any?) 
    print("Successful")

另一答案

在Swift 4.2中编写此示例代码,以编程方式添加Button。

override func viewDidLoad() 
    super.viewDidLoad()
        let myButton = UIButton(frame: CGRect(x: 100, y: 100, width: 100, height: 50))
        myButton.backgroundColor = .green
        myButton.setTitle("Hello UIButton", for: .normal)
        myButton.addTarget(self, action: #selector(myButtonAction), for: .touchUpInside)
        self.view.addSubview(myButton)


 @objc func myButtonAction(sender: UIButton!) 
    print("My Button tapped")

另一答案
    // UILabel:
    let label = UILabel()
    label.frame = CGRectMake(35, 100, 250, 30)
    label.textColor = UIColor.blackColor()
    label.textAlignment = NSTextAlignment.Center
    label.text = "Hello World"
    self.view.addSubview(label)

    // UIButton:
    let btn: UIButton = UIButton(type: UIButtonType.Custom) as UIButton
    btn.frame = CGRectMake(130, 70, 60, 20)
    btn.setTitle("Click", forState: UIControlState.Normal)
    btn.setTitleColor(UIColor.blackColor(), forState: .Normal)
    btn.addTarget(self, action:Selector("clickAction"), forControlEvents: UIControlEvents.TouchUpInside)
    view.addSubview(btn)


    // Button Action:
    @IBAction func clickAction(sender:AnyObject)
    
        print("Click Action")
    
另一答案

Swift:Uibutton以编程方式创建

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
self.view.addSubview(myButton)
另一答案

第1步:制作一个新项目

enter image description here

第2步:在ViewController.swift中

import UIKit

class ViewController: UIViewController 

    override func viewDidLoad() 
        super.viewDidLoad()

        // CODE
        let btn = UIButton(type: UIButtonType.System) as UIButton        
        btn.backgroundColor = UIColor.blueColor()
        btn.setTitle("CALL TPT AGENT", forState: UIControlState.Normal)
        btn.frame = CGRectMake(100, 100, 200, 100)
        btn.addTarge

以上是关于如何以编程方式创建按钮?的主要内容,如果未能解决你的问题,请参考以下文章

如何以编程方式创建重叠栏按钮项

如何创建可以从以编程方式创建的按钮调用的 segue?

如何在swift中使用for循环以编程方式创建按钮

当内部按钮触摸时,如何从 Superview 中删除以编程方式创建的子视图?

如何更改以编程方式创建的一组 UIButton 的标题颜色?

如何以编程方式添加文本字段和按钮?