1

I am using a UIViewController subclass in an Xcode Swift playground. The view of the controller is apparently reporting as double-sized. Am I doing something wrong or did I stumble into a bug?

When I place a yellow subview of half the width into the view, it's covering the entire width.

Xcode Version 8.2.1 (8C1002)

import UIKit
import PlaygroundSupport


class ViewController : UIViewController {
  override func viewDidLoad() {
    title = "Double Frame"
    super.viewDidLoad()
    view.frame // inspecting the frame size: 768 x 1024

    let halfV = UIView(frame: CGRect(x: 0, y: 200,
                                     width: Int(view.frame.width / 2),
                                     height: Int(view.frame.width / 2)))
    halfV.backgroundColor = UIColor.yellow
    view.addSubview(halfV)

  }
}
PlaygroundPage.current.liveView = UINavigationController(rootViewController: ViewController())

enter image description here

bshirley
  • 8,217
  • 1
  • 37
  • 43
  • 2
    Your view controller's view is getting resized sometime after `viewDidLoad` is called. You need to size your yellow with constraints/autoresizing masks/some other method that will resize it when it's superview resizes. – dan Feb 02 '17 at 18:37

1 Answers1

2

You view controller's view is going to be sized using auto layout. The auto layout pass is going to occur after viewDidLoad. If you want to programmatic size your subview by referencing the frame of your view controller's view, you should move that code to viewDidLayoutSubviews. For example:

class ViewController : UIViewController {
    let halfV = UIView()

    override func viewDidLoad() {
        title = "Double Frame"
        super.viewDidLoad()
        view.frame // inspecting the frame size: 768 x 1024


        halfV.backgroundColor = UIColor.yellow
        view.addSubview(halfV)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        halfV.frame = CGRect(x: 0, y: 200,
               width: CGFloat(view.frame.width / 2),
               height: CGFloat(view.frame.width / 2))
    }
}
PlaygroundPage.current.liveView = UINavigationController(rootViewController: ViewController())
beyowulf
  • 15,101
  • 2
  • 34
  • 40