简体   繁体   中英

How to set the frame of a ViewController without storyboard

I want to create a view (eg 100x100) with ViewController without using the storyboard. I am wondering what the best way to declare the frame of the ViewController is.

I tried:

class MyLittleViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()        
        view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    }
}

And I tried to see this view on my MainViewController:

override func viewDidLoad() {
    super.viewDidLoad()

    let myLittleView = MyLittleViewController()
    myLittleView.willMove(toParent: self)
    myLittleView.view.translatesAutoresizingMaskIntoConstraints = false
    myLittleView.view.backgroundColor = .red
    view.addSubview(myLittleView.view)
    // enable auto-sizing (for example, if the device is rotated)
    myLittleView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    self.addChild(myLittleView)
    myLittleView.didMove(toParent: self)

    myLittleView.view.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
    myLittleView.view.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}

It doesn't work like I expected because the little view doesn't appear on the main view. Any hints?

You shouldn't mix frame layout with auto-layout set a width & height constraints also

NSLayoutConstraint.activate([
    myLittleView.view.centerXAnchor.constraint(equalTo: view.centerXAnchor),
    myLittleView.view.centerYAnchor.constraint(equalTo: view.centerYAnchor),
    myLittleView.view.heightAnchor.constraint(equalToConstant:100),
    myLittleView.view.widthAnchor.constraint(equalToConstant:100)
])

OR

myLittleView.view.frame = CGRect(x: 0, y: 0, width: 100, height: 100) 
myLittleView.view.center = view.center

OR

override func loadView() {
    view = UIView()
    view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
}

Edit:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
           self.view.backgroundColor = .green
        let myLittleView = MyLittleViewController()
        myLittleView.willMove(toParent: self)
        myLittleView.view.backgroundColor = .red
        view.addSubview(myLittleView.view)
        // enable auto-sizing (for example, if the device is rotated)
        myLittleView.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        self.addChild(myLittleView)
        myLittleView.didMove(toParent: self)
        myLittleView.view.center = view.center
    }
}

class MyLittleViewController: UIViewController {

    override func loadView() {
        view = UIView()
        view.frame = CGRect(x: 0, y: 0, width: 100, height: 100)
    }

}

Another way to set frame of the view controller is override method loadView() and set the view frame like this

func loadView() {
   view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM