如何UIButton
在Swift中以编程方式创建图形元素(如a )?我试图在视图中创建并添加按钮,但无法进行.
您可以通过这种方式以编程方式添加UIButton,UIlable和UITextfield.
UIButton代码
// var button = UIButton.buttonWithType(UIButtonType.System) as UIButton let button = UIButton(type: .System) // let preferred over var here button.frame = CGRectMake(100, 100, 100, 50) button.backgroundColor = UIColor.greenColor() button.setTitle("Button", forState: UIControlState.Normal) button.addTarget(self, action: "Action:", forControlEvents: UIControlEvents.TouchUpInside) self.view.addSubview(button)
UILabel代码
var label: UILabel = UILabel() label.frame = CGRectMake(50, 50, 200, 21) label.backgroundColor = UIColor.blackColor() label.textColor = UIColor.whiteColor() label.textAlignment = NSTextAlignment.Center label.text = "test label" self.view.addSubview(label)
UITextField代码
var txtField: UITextField = UITextField() txtField.frame = CGRectMake(50, 70, 200, 30) txtField.backgroundColor = UIColor.grayColor() self.view.addSubview(txtField)
希望这对你有所帮助.
var sampleButton:UIButton? override func viewDidLoad() { super.viewDidLoad() } override func viewDidAppear(animated: Bool) { sampleButton = UIButton(type: .RoundedRect) //sampleButton.frame = CGRect(x:50, y:500, width:70, height:50) sampleButton!.setTitle("Sample \n UI Button", forState: .Normal) sampleButton!.titleLabel?.lineBreakMode = .ByWordWrapping sampleButton!.titleLabel?.textAlignment = .Center sampleButton!.setTitleColor(UIColor.whiteColor(), forState: .Normal) sampleButton!.layer.cornerRadius = 6 sampleButton!.backgroundColor = UIColor.redColor().colorWithAlphaComponent(0.6) sampleButton?.tintColor = UIColor.brownColor() //Add padding around text sampleButton!.titleEdgeInsets = UIEdgeInsetsMake(-10,-10,-10,-10) sampleButton!.contentEdgeInsets = UIEdgeInsetsMake(5,5,5,5) //Action set up sampleButton!.addTarget(self, action: "sampleButtonClicked", forControlEvents: .TouchUpInside) self.view.addSubview(sampleButton!) //Button Constraints: sampleButton!.translatesAutoresizingMaskIntoConstraints = false //To anchor above the tab bar on the bottom of the screen: let bottomButtonConstraint = sampleButton!.bottomAnchor.constraintEqualToAnchor(bottomLayoutGuide.topAnchor, constant: -20) //edge of the screen in InterfaceBuilder: let margins = view.layoutMarginsGuide let leadingButtonConstraint = sampleButton!.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor) bottomButtonConstraint.active = true leadingButtonConstraint.active = true } func sampleButtonClicked(){ print("sample Button Clicked") }
以下是UIButton
使用targetAction以编程方式添加的完整解决方案.
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
.
API没有改变 - 只有语法有.您可以UIButton
像这样添加并添加:
var button = UIButton(frame: CGRectMake(0, 0, 50, 50)) self.view.addSubview(button) // assuming you're in a view controller
您可以像这样创建,也可以像这样添加动作....
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") }
斯威夫特3
let btn = UIButton(type: .custom) as UIButton btn.backgroundColor = .blue btn.setTitle("Button", for: .normal) btn.frame = CGRect(x: 100, y: 100, width: 200, height: 100) btn.addTarget(self, action: #selector(clickMe), for: .touchUpInside) self.view.addSubview(btn) func clickMe(sender:UIButton!) { print("Button Clicked") }
产量
在Swift 2和iOS 9.2.1中
var button: UIButton = UIButton(type: UIButtonType.Custom) as UIButton self.button.frame = CGRectMake(130, 70, 60, 20) self.button.setTitle("custom button", forState: UIControlState.Normal) self.button.addTarget(self, action:"buttonActionFuncName", forControlEvents: UIControlEvents.TouchUpInside) self.button.setTitleColor(UIColor.blackColor(), forState: .Normal) self.button.layer.borderColor = UIColor.blackColor().CGColor self.button.titleLabel?.font = UIFont(name: "Helvetica-Bold", size: 13) self.view.addSubview(self.button)
对于Swift 3
let button = UIButton() button.frame = CGRect(x: self.view.frame.size.width - 60, y: 60, width: 50, height: 50) button.backgroundColor = UIColor.red button.setTitle("your Button Name", for: .normal) button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) self.view.addSubview(button) func buttonAction(sender: UIButton!) { print("Button tapped") }
对于Swift 4
let button = UIButton() button.frame = CGRect(x: self.view.frame.size.width - 60, y: 60, width: 50, height: 50) button.backgroundColor = UIColor.red button.setTitle("Name your Button ", for: .normal) button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside) self.view.addSubview(button) @objc func buttonAction(sender: UIButton!) { print("Button tapped") }
如何使用Swift 3.0做到这一点.
func createButton() { let button = UIButton(type: .system) button.frame = CGRect(x: 100.0, y: 100.0, width: 100.0, height: 100.0) button.setTitle(NSLocalizedString("Button", comment: "Button"), for: .normal) button.backgroundColor = .green button.addTarget(self, action: #selector(buttonAction(sender:)), for: .touchUpInside) view.addSubview(button) } @objc func buttonAction(sender: UIButton) { print("Button pushed") }
在viewDidLoad
//添加Button中添加此代码
var button=UIButton(frame: CGRectMake(150, 240, 75, 30)) button.setTitle("Next", forState: UIControlState.Normal) button.addTarget(self, action: "buttonTapAction:", forControlEvents: UIControlEvents.TouchUpInside) button.backgroundColor = UIColor.greenColor() self.view.addSubview(button)
将此功能写在外面,当您点击按钮时会调用此功能
func buttonTapAction(sender:UIButton!) { println("Button is working") }