I created an UIImageView and UIButton programmatically to setup constraints and I am not sure how to add this to my storyboard. I want the UIImageView and UIButton to show up on the storyboard after it has been created programmatically. How do I go about this? I'm new to swift so sorry if it's really simple.
I tried adding an @IBOutlet from the storyboard before adding in the constraints and modifying its text/image but it doesn't work.
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
}
}
I get the expected result programmatically but I need to add it to the storyboard to perform segues.
For storybard link declare like
@IBOutlet weak var logoImageView: UIImageView!
@IBOutlet weak var signUpButton: UIButton!
Then add the items inside the storyboard and link them to above vars
BTW you don't have to use storbaord to perform a segue you can use present
/ push
programmatically
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.