How to make frame height of object 80 percent of view controller

I want to make the object box 80 percent height of the view controller and the width should be a 100 percent. The object box should be pinned to the top with the 20 percent of space being pinned to the bottom.

import UIKit

class ViewController: UIViewController {
    var box = UIImageView()

    override func viewDidLoad() {
        // Do any additional setup after loading the view.

        box.backgroundColor = .blue
        box.frame = self.view.frame


Hi @timswift,
Do you know how to use autolayout constraints? That is the best way to do this.

another way you can do this

var newFrame = UIScreen.main.bounds // or use self.view.frame
newFrame.size.height = newFrame.height * 0.8
box.frame = newFrame


This topic was automatically closed after 166 days. New replies are no longer allowed.