我如何以编程方式创建图形元素(如UIButton)在Swift?我试图创建和添加按钮到一个视图,但不能。
当前回答
你可以通过这种方式添加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)
希望这对你有帮助。
其他回答
下面是一个完整的解决方案,以编程方式添加一个UIButton与targetAction。 斯威夫特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,上述方法仍然有效。
将此代码添加到viewDidLoad中 / /添加按钮
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")
}
你可以这样创建,也可以这样添加动作....
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")
}
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();
}
从storyboard创建UIButton: 1 -将UIButton对象从对象库拖到storyboard文件中的ViewController 2 -节目助理编辑 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")
}
推荐文章
- 更改UITextField和UITextView光标/插入符颜色
- 如何使用@Binding变量实现自定义初始化
- 'Project Name'是通过优化编译的——步进可能会表现得很奇怪;变量可能不可用
- Swift设置为Array
- 如何设置回退按钮文本在Swift
- 我如何能在Swift扩展类型化数组?
- Swift类错误:属性未在super处初始化。init调用
- 模拟器慢动作动画现在打开了吗?
- 如何为TableView创建NSIndexPath
- 滑动删除和“更多”按钮(就像iOS 7的邮件应用程序)
- 使UINavigationBar透明
- 如何改变推和弹出动画在一个基于导航的应用程序
- 删除/重置核心数据中的所有条目?
- Swift to Objective-C头未在Xcode 6中创建
- setNeedsLayout vs. setNeedsUpdateConstraints和layoutIfNeeded vs. updateConstraintsIfNeeded