1

I want to transform UIView from portrait to landscape mode and make it fullscreen. How can I do that?

CGFloat degrees = 90;
CGAffineTransform transform = CGAffineTransformMakeRotation((M_PI * degrees / 180.0f));

[UIView animateWithDuration:0.3f animations:^{
    transitionView.transform = transform;
    // How to set the origin to make it fullscreen?
} completion:^(BOOL finished) {
}];
noinput
  • 411
  • 3
  • 12

3 Answers3

1
  1. Change view frame by swapping height and width
  2. Rotate by -90 degrees (counter clockwise)
  3. Translate to match new origin (because rotation is performed around the view center point)

    UIView.animate(withDuration: 0.3) {
        self.frame = CGRect(origin: .zero, size: CGSize(width: UIScreen.main.bounds.height,
                                                                height: UIScreen.main.bounds.width))
    
        let rotate = CGAffineTransform(rotationAngle: -.pi / 2)
        let center = self.view.center
        let translate = CGAffineTransform(translationX: center.x + center.y - self.bounds.width,
                                          y: center.y - center.x)
        self.transform = translate.concatenating(rotate)
    }
    
karama
  • 254
  • 3
  • 5
0

enter image description here

You have to select the view and give constraints to that view such that it sticks with the margin (as shown below). Then it will automatically stretch to full screen.

You can also use size classes, and set different constraints for landscape and portrait mode.

chinmayan
  • 1,304
  • 14
  • 13
0

Here the solution in swift 4

if UIDeviceOrientationIsLandscape(UIDevice.current.orientation){
        let screenSize = UIScreen.main.bounds
        let screenWidth = screenSize.width
        let screenHeight = screenSize.height
        videoView.frame = CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight)
        playerLayer.frame = videoView.bounds
}
Ruben Nahatakyan
  • 372
  • 3
  • 13