Ios 如何打开已编程的UIButton(swift4)
我正在尝试使用编程约束创建一个按钮。我尽量不使用故事板。我打开按钮时遇到问题。我该怎么做Ios 如何打开已编程的UIButton(swift4),ios,swift4,optional,nslayoutconstraint,unwrap,Ios,Swift4,Optional,Nslayoutconstraint,Unwrap,我正在尝试使用编程约束创建一个按钮。我尽量不使用故事板。我打开按钮时遇到问题。我该怎么做 import UIKit var aa: [NSLayoutConstraint] = [] class ViewController: UIViewController { var btn: UITextField! override func viewDidLoad() { super.viewDidLoad() self.view.addSub
import UIKit
var aa: [NSLayoutConstraint] = []
class ViewController: UIViewController {
var btn: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
self.view.addSubview(btn)
let leadingc2 = btn.widthAnchor.constraint(equalToConstant: 80)
let trailingC2 = btn.heightAnchor.constraint(equalToConstant: 50)
let topc2 = btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -50)
let bottomc2 = btn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
aa = [leadingc2,trailingC2,topc2,bottomc2]
NSLayoutConstraint.activate(aa)
}
}
你不需要打开它。在使用它之前,您需要实例化它
override func viewDidLoad() {
super.viewDidLoad()
btn = UITextField() // Create the button like this before using it.
self.view.addSubview(btn)
btn.translatesAutoresizingMaskIntoConstraints = false
let leadingc2 = btn.widthAnchor.constraint(equalToConstant: 80)
let trailingC2 = btn.heightAnchor.constraint(equalToConstant: 50)
let topc2 = btn.centerXAnchor.constraint(equalTo: self.view.centerXAnchor, constant: -50)
let bottomc2 = btn.centerYAnchor.constraint(equalTo: self.view.centerYAnchor, constant: -250)
aa = [leadingc2,trailingC2,topc2,bottomc2]
NSLayoutConstraint.activate(aa)
}
使用声明的任何变量代码>将强制展开,这意味着如果您忘记创建实例并使用变量,它将抛出错误并使您的应用程序崩溃。使用以下代码:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(button)
setupConstraints()
}
lazy var button: UIButton = {
let button = UIButton(type: .system)
button.setTitle("Button", for: .normal)
button.backgroundColor = .blue
//your action here
button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
return button
}()
private func setupConstraints() {
button.translatesAutoresizingMaskIntoConstraints = false
let top = NSLayoutConstraint(item: button, attribute: .top, relatedBy: .equal, toItem: view, attribute: .top, multiplier: 1, constant: 100)
let left = NSLayoutConstraint(item: button, attribute: .left, relatedBy: .equal, toItem: view, attribute: .left, multiplier: 1, constant: 50)
let right = NSLayoutConstraint(item: button, attribute: .right, relatedBy: .equal, toItem: view, attribute: .right, multiplier: 1, constant: -50)
let height = NSLayoutConstraint(item: button, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 50)
view.addConstraints([top, left, right, height])
}
@objc func buttonAction() {
print("Button has pressed")
}
}
当使用button函数无法将“()->”类型的值转换为指定类型的“UIButton”@AlexKolovatov时,我遇到了此错误,是哪个约束导致了此错误?您刚刚忘记添加此行。btn.translatesAutoresizingMaskIntoConstraints=false@AlexKolovatov是的,但答案不是约束。它用于按钮崩溃,因为它没有实例。我只是添加了OP的其他代码,这样他就能更好地理解在哪里做。但谢谢你指出这一点。如果你想的话,你可以用那一行编辑答案。当然你是对的。但我记得当我还是个初学者时,每一个错误都让我头疼:)