我如何以编程方式创建图形元素(如UIButton)在Swift?我试图创建和添加按钮到一个视图,但不能。
当前回答
如何使用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")
}
其他回答
API没有改变——只有语法改变了。你可以创建一个UIButton并像这样添加它:
var button = UIButton(frame: CGRectMake(0, 0, 50, 50))
self.view.addSubview(button) // assuming you're in a view controller
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的扩展。斯威夫特5。
let button: UIButton = UIButton()
override func viewDidLoad() {
super.viewDidLoad()
button.setup(title: "OK", x: 100, y: 430, width: 220, height: 80, color: .yellow)
buttonD.setTitleColor(.black, for: .normal)
}
extension UIButton {
func setup(title: String, x: CGFloat, y: CGFloat, width: CGFloat, height: CGFloat, color: UIColor){
frame = CGRect(x: x, y: y, width: width, height: height)
backgroundColor = color
setTitle(title , for: .normal)
}
}
你可以通过这种方式添加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)
希望这对你有帮助。
将此代码添加到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")
}
推荐文章
- 改变UISegmentedControl的字体大小
- 我可以强制UITableView隐藏分隔符之间的空单元格吗?
- 获取用户当前位置/坐标
- 获得推送通知,而应用程序在前台iOS
- 如何取消选定的UITableView单元格?
- 设置自定义UITableViewCells的高度
- 在SwiftUI中创建一个VStack填充屏幕宽度
- 移动文本字段时,键盘出现迅速
- 让iPhone震动
- 将NSString转换为NSDate(然后再转换回来)
- 如何在swift中逆向循环迭代?
- applicationwillenter前台vs. applicationDidBecomeActive, applicationWillResignActive vs. applicationDidEnterBackground
- 如何在扑动中格式化日期时间
- Xcode 8 / Swift 3: "Expression of type UIViewController?未使用”警告
- 我如何使成员初始化公共,默认情况下,在Swift的结构?