xcode – The buttons will not show up in the IOS simulator, can anyone assist? (Code Provided Below)

Take a real hard look at…

private let loginButton: UIButton = {
    let loginbutton = UIButton()
    loginbutton.setTitle("Log In", for: .normal)
    loginbutton.layer.masksToBounds = true
    loginbutton.layer.cornerRadius = Constants.cornerRadius
    loginbutton.backgroundColor = .link
    loginbutton.setTitleColor(.white, for: .normal)
    return UIButton()
}()

see anything wrong? Maybe focus in on return UIButton()

So, changing it to…

private let loginButton: UIButton = {
    let loginbutton = UIButton()
    loginbutton.setTitle("Log In", for: [])
    loginbutton.layer.masksToBounds = true
    loginbutton.layer.cornerRadius = Constants.cornerRadius
    loginbutton.backgroundColor = .link
    loginbutton.setTitleColor(.white, for: [])
    return loginbutton
}()

will produce…

//
//  ViewController.swift
//  StackOverflow
//
//  Created by Shane Whitehead on 4/5/2022.
//

import UIKit

class ViewController: UIViewController, UITextFieldDelegate{
    
    struct Constants {
        static let cornerRadius: CGFloat = 8.0
    }
    //Declaration of login fields
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        loginButton.addTarget(self, action: #selector(didTaploginButton), for: .touchUpInside)
        createAccountButton.addTarget(self, action: #selector(didTapcreateAccountButton), for: .touchUpInside)
        termsButton.addTarget(self, action: #selector(didTaptermsButton), for: .touchUpInside)
        privacyButton.addTarget(self, action: #selector(didTapprivacyButton), for: .touchUpInside)
        usernameEmailField.delegate = self
        passwordField.delegate = self
        
        addSubviews()
        view.backgroundColor = .systemBackground
        // Do any additional setup after loading the view.
    }
    
    private let usernameEmailField: UITextField = {
        let field = UITextField()
        field.placeholder = "Username or Email"
        field.returnKeyType = .next
        field.leftViewMode = .always
        field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.layer.masksToBounds = true
        field.layer.cornerRadius = Constants.cornerRadius
        field.backgroundColor = .secondarySystemBackground
        
        return field
    }()
    
    private let passwordField: UITextField = {
        let field = UITextField()
        field.placeholder = "Password"
        field.returnKeyType = .next
        field.leftViewMode = .always
        field.leftView = UIView(frame: CGRect(x: 0, y: 0, width: 10, height: 0))
        field.autocapitalizationType = .none
        field.autocorrectionType = .no
        field.layer.masksToBounds = true
        field.layer.cornerRadius = Constants.cornerRadius
        field.backgroundColor = .secondarySystemBackground
        return field
    }()
    
    private let loginButton: UIButton = {
        let loginbutton = UIButton()
        loginbutton.setTitle("Log In", for: [])
        loginbutton.layer.masksToBounds = true
        loginbutton.layer.cornerRadius = Constants.cornerRadius
        loginbutton.backgroundColor = .link
        loginbutton.setTitleColor(.white, for: [])
        return loginbutton
    }()
    
    private let termsButton: UIButton = {
        let button = UIButton()
        button.setTitle("Terms of Service", for: [])
        button.setTitleColor(.secondaryLabel, for: [])
        return button
    }()
    
    private let privacyButton: UIButton = {
        return UIButton()
    }()
    
    private let createAccountButton: UIButton = {
        let button = UIButton()
        button.setTitleColor(.lightGray, for: [])
        button.setTitle("New User? Create an Account", for: [])
        button.backgroundColor = .systemBlue
        button.layer.cornerRadius = Constants.cornerRadius
        button.setTitleColor(.label, for: .normal)
        return button
    }()
    //Picture behind the Logo
    private let headerView: UIView = {
        let header = UIView()
        header.clipsToBounds = true
        let backgroundImageView = UIImageView(image: UIImage(named: "Bluewater"))
        header.addSubview(backgroundImageView)
        return header
    }()
    
    
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
        //assigning frames
        
        headerView.frame = CGRect(x: 0,
                                  y: 0.0,
                                  width: view.width,
                                  height: view.height/3.0
        )
        
        usernameEmailField.frame = CGRect(x: 25,
                                          y: headerView.bottom + 10,
                                          width: view.width-50,
                                          height: 52.0
        )
        
        passwordField.frame = CGRect(x: 25,
                                     y: usernameEmailField.bottom + 10,
                                     width: view.width-50,
                                     height: 52.0
        )

        loginButton.frame = CGRect(x: 25,
                                   y: passwordField.bottom + 10,
                                   width: view.width-50,
                                   height: 52.0
        )
        
        createAccountButton.frame = CGRect(x: 25,
                                           y: loginButton.bottom + 10,
                                           width: view.width-50,
                                           height: 52.0
        )
        termsButton.frame = CGRect(x: 10, y: view.height-view.safeAreaInsets.bottom-100, width: view.width-20, height: 50)
        
        configurationView()
    }
    
    
    private func configurationView() {
        guard headerView.subviews.count == 1 else {
            return
        }
        guard let backgroundView = headerView.subviews.first else {
            return
        }
        backgroundView.frame = headerView.bounds
        
        //Pic Pool Logo
        let imageView = UIImageView(image: UIImage(named: "ClearLogo"))
        headerView.addSubview(imageView)
        imageView.contentMode = .scaleAspectFit
        imageView.frame = CGRect(x: headerView.width/4.0,
                                 
                                 y: view.safeAreaInsets.top,
                                 width: headerView.width/2.0,
                                 height: headerView.height - view.safeAreaInsets.top)
    }
    
    
    private func addSubviews(){
        view.addSubview(usernameEmailField)
        view.addSubview(passwordField)
        view.addSubview(loginButton)
        view.addSubview(termsButton)
        view.addSubview(privacyButton)
        view.addSubview(createAccountButton)
        view.addSubview(headerView)
    }
    
    @objc private func didTaploginButton() {}
    @objc private func didTaptermsButton() {}
    @objc private func didTapprivacyButton() {}
    @objc private func didTapcreateAccountButton() {}
}

extension UIView {
    var width: CGFloat {
        return bounds.width
    }
    
    var height: CGFloat {
        return bounds.height
    }
    
    var bottom: CGFloat {
        return frame.minY + bounds.height
    }
}

I’d also, strongly, recommend either making the time to learn how to use auto layout OR SwiftUI instead

Leave a Comment