Search code examples
iosswiftuiviewcontrollersubview

Why does UIView fill the superView?


I am creating a subview programmatically that I would like to be positioned over a superView, but I do not want it to fill the enter superView.

I have been checking around to see if this question has been asked before but for some reason, I can only find answers to how to fill the entire view.

I would really appreciate it if someone could help critique my code and explain how to position a subView instead of filling the entire superview.

class JobViewController: UIViewController {
    var subView : SubView { return self.view as! SubView }
    var requested = false

    let imageView: UIImageView = {
        let iv = UIImageView(image: #imageLiteral(resourceName: "yo"))
        iv.contentMode = .scaleAspectFill
        iv.isUserInteractionEnabled = true
        iv.translatesAutoresizingMaskIntoConstraints = false
        return iv
    }()

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(imageView)
        imageView.fillSuperview()

        subView.requestAction = { [ weak self ] in
            guard let strongSelf = self else { return }
            strongSelf.requested = !strongSelf.requested
            if strongSelf.requested {
                UIView.animate(withDuration: 0.5, animations: {
                    strongSelf.subView.Request.setTitle("Requested", for: .normal)
                    strongSelf.subView.contentView.backgroundColor = UIColor.red.withAlphaComponent(0.5)
                })
            } else {
                UIView.animate(withDuration: 0.5, animations: {
                    strongSelf.subView.Request.setTitle("Requested", for: .normal)
                    strongSelf.subView.contentView.backgroundColor = UIColor.blue
                })
            }
        }
    }

    override func loadView() {
     // I know the issue lies here, but how would I go about setting the frame of the subview to just be positioned on top of the mainView?
        self.view = SubView(frame: UIScreen.main.bounds)
    }
}

I have my subView built in a separate file, I am not sure whether or not I would need its information since It is just what is inside of the subview.


Solution

  • You should add your subView as a subview of self.view and not set it equal your main view. And then set the constraints accordingly.

    override func viewDidLoad() {
        self.view.addSubview(subView)
        subview.translatesAutoresizingMaskIntoConstraint = false
        addSubview.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0).isActive = true
        addSubview.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0).isActive = true
        addSubview.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0).isActive = true
        addSubview.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0).isActive = true
    }
    

    Regarding your initialisation problem try:

    var subView = SubView()
    

    I hope I understood your question correct.