noinput
noinput

Reputation: 411

How to transform UIView to landscape and fullscreen?

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) {
}];

Upvotes: 1

Views: 2551

Answers (3)

karama
karama

Reputation: 294

  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)
    }
    

Upvotes: 1

Ruben Nahatakyan
Ruben Nahatakyan

Reputation: 408

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
}

Upvotes: 0

chinmayan
chinmayan

Reputation: 1384

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.

Upvotes: 0

Related Questions