0

I'm turning my hand from years of Java / C++ / C to trying to learn Swift and how to develop on Apple.

I've tried a lot of tutorials and for what I want to achieve I need to use a splitview controller.

I need to have a few different detail views and can't work out how to do this via storyboard and thought I'd try to do it with code.

Following a tutorial and several google searches Im hitting a problem which I realise is me being a newbiee and ask for your help.

To keep things simple I created a test viewcontroller class and set it in the storyboard no problem.

import UIKit;

class tesctVC : UIViewController {
    var scoreLabel: UILabel!
    
    override func loadView() {
        view = UIView()
        view.backgroundColor = .white
        
        scoreLabel = UILabel()
        scoreLabel.translatesAutoresizingMaskIntoConstraints = false
        scoreLabel.textAlignment = .right
        scoreLabel.text = "Score: 0"
        view.addSubview(scoreLabel)
        // more code to come!
    }
}

When I change to background color it shows as expected.

But when I try to add anything else, I've tried UILabels, UIButtons etc, they do not show.

Would you please give me some pointers?

halfer
  • 19,824
  • 17
  • 99
  • 186
Jeff Baker
  • 1
  • 1
  • 5

1 Answers1

0

Thanks again. I thought this may help others.

This is still WIP but shows some hints I found along the way.

I want to add a bunch of labels & buttons etc.

This in so far as is written does the job. It needs more formatting and so on.

The extension is needed to set a stacks background color. I think someone explained its because a StackView is not a drawable object.

extension UIStackView {
    func addBackground(color: UIColor) {
        let subView = UIView(frame: bounds)
        subView.backgroundColor = color
        subView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        insertSubview(subView, at: 0)
    }
}


class DetailViewController: UIViewController , UIWebViewDelegate {


    
    var usernameEdt: UITextField!
    var passwordEdt: UITextField!
    var statusLabel: UILabel!
    var LogubButton = UIButton()
           
    
    // Draw the login page
    func loginPage() {
      
        // Create the top level statck
        let stackView = UIStackView()
        stackView.addBackground(color: .white)
        stackView.axis = .vertical
        stackView.alignment = .center // .leading .firstBaseline .center .trailing .lastBaseline
        stackView.distribution = .fillEqually // .fillEqually .fillProportionally .equalSpacing .equalCentering
        
        // Add the title
        let label = UILabel()
        label.frame = CGRect(x: 150, y: 200, width: 200, height: 20)
        label.font = UIFont.boldSystemFont(ofSize: 20.0)
        label.text = "Login Details"
        label.textColor = .blue
        
        stackView.addArrangedSubview(label)
        
   
        // Add Username
        usernameEdt = UITextField()
        usernameEdt.placeholder = "Enter Your Username"
       
        stackView.addArrangedSubview(usernameEdt)
       
        
        // Add Password
        passwordEdt = UITextField()
        passwordEdt.placeholder = "Enter Your Password"
        stackView.addArrangedSubview(passwordEdt)
 
        
        self.view = stackView
    }
  

Thanks Jeff

Jeff Baker
  • 1
  • 1
  • 5