0

I'm working on this project and I have one scene on the Storyboard.

The scene is a TableViewController.

  • The table view have a custom prototype cell (linked to CustomCell.swift).

    • Inside the prototype cell there's a label and a custom UIView (linked to CustomView.swift). These elements have layout constraints relative to the contentView of the prototype cell.

Now, I want the stuff being drawn on my custom view to change when the size of the view changes, so that when the device rotates it is adjusted to that new cell width. Because of the constraints, the frame of CustomView will change when the CustomCell changes size, after the device is rotated. In order to detect this, I added two property observers to CustomView.swift:

override var frame: CGRect {
    didSet {
        print("Frame was set!")
        updateDrawing()
    }
}

override var bounds: CGRect {
    didSet {
        print("Bounds were set!")
        updateDrawing()
    }
}

When running the project, the second observer works fine when I rotate the device. The first observer does not. My question is why does the first observer not detect that the frame has changed?

CrazyJony
  • 925
  • 1
  • 9
  • 12

1 Answers1

2

.frame is computed from the .bounds and the .center of the view (and the transform), so it does not change. In order to react to rotation override this (starting from iOS8):

override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    super.viewWillTransitionToSize(size, withTransitionCoordinator: coordinator)

    coordinator.animateAlongsideTransition({ (coordinator) -> Void in
        // do your stuff here
        // here the frame has the new size
    }, completion: nil)
}
Darko
  • 9,655
  • 9
  • 36
  • 48