Search code examples
iosuiviewcalayernslayoutconstraintuiinterfaceorientation

Resizing layer when interface orientation changes?


I am developing some simple app that supports both portrait mode and landscape mode. I have a view UIView that has its constraints set (called videoView), and that resize properly when interface orientation changes. On top of that view i have added custom layer that. That layer should follow view's frame, so when interface orientation changes, it should change too.

Problem is layers don't have constraints. I have done something like this:

- (void) viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _previewLayer.frame = CGRectMake(0, 0, self.videoView.frame.size.width, self.videoView.frame.size.height);
}

It resizes layer properly, but it doesn't look good. First videoView resizes, and then after some delay layer resizes. I would like it to be one smooth transition. How can i accomplish this?


Solution

  • override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
    if UIDevice.currentDevice().orientation.isLandscape.boolValue {
        print("Landscape")
    } else {
        print("Portrait")
    }
    

    This will tell you if the device will is going to rotate before it happens. This is where you can run your code.