如何以编程方式创建按钮?
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
被宣布为private
或internal
,上述仍然有效。
您可以像这样创建,也可以像这样添加动作....
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步:制作一个新项目
第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以上是关于如何以编程方式创建按钮?的主要内容,如果未能解决你的问题,请参考以下文章
当内部按钮触摸时,如何从 Superview 中删除以编程方式创建的子视图?