我以编程方式创建了UIImageView和UIButton来设置约束,但不确定如何将其添加到情节提要中。我希望UIImageView和UIButton以编程方式创建后显示在情节提要上。我该怎么办?我是新手,如果真的很简单,抱歉。
在添加约束并修改其文本/图像之前,我尝试从情节提要中添加一个@IBOutlet,但这不起作用。
class LaunchViewController: UIViewController {
let logoImageView: UIImageView = {
let imageView = UIImageView()
let logoImage = UIImage(named: "logo")
imageView.image = logoImage
imageView.translatesAutoresizingMaskIntoConstraints = false
return imageView
}()
let signUpButton: UIButton = {
let signUpButton = UIButton.init(type: .system)
signUpButton.frame = CGRect(x: 20, y: 20, width: 2, height: 20)
signUpButton.setTitle("Sign Up.", for: .normal)
signUpButton.addTarget(self, action: #selector(buttonClicked(sender:)), for: .touchUpInside)
signUpButton.translatesAutoresizingMaskIntoConstraints = false
return signUpButton
}()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.addSubview(logoImageView)
view.addSubview(signUpButton)
setUpLayout()
}
@objc func buttonClicked(sender:UIButton!){
print("..")
}
private func setUpLayout(){
logoImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
logoImageView.topAnchor.constraint(equalTo: view.topAnchor, constant: 100).isActive = true
logoImageView.widthAnchor.constraint(equalToConstant: 220).isActive = true
logoImageView.heightAnchor.constraint(equalToConstant: 200).isActive = true
signUpButton.topAnchor.constraint(equalTo: logoImageView.bottomAnchor, constant: 300).isActive = true
signUpButton.leftAnchor.constraint(equalTo: view.leftAnchor,constant: 250).isActive = true
signUpButton.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true
signUpButton.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
}
}
我以编程方式获得了预期的结果,但是我需要将其添加到情节提要中以执行搜索。
答案 0 :(得分:0)
对于Storybard链接,声明类似
@IBOutlet weak var logoImageView: UIImageView!
@IBOutlet weak var signUpButton: UIButton!
然后将项目添加到情节提要中,并将其链接到上方的vars
顺便说一句,您不必使用storbaord来执行搜索,您可以通过编程方式使用present
/ push