A.S
A.S

Reputation: 816

UIView not updating constraints in landscape mode

I have a UIViewController which has a UIView that plays a video.

The constraints given to this UIView are as follows.

Trailing space to : Superview

Leading space to :Superview

Top space to Superview

Bottom space to : Bottom layout guide

When I run the app in portrait mode it works fine. When I change the orientation to landscape by turning the iPhone it retains the same width and height as that of the portrait mode and does not play the video in full screen

The current orientation in the storyboard is portrait in iPhone 7.

When I rotate the orientation in the storyboard board to landscape and run the app it runs in full screen as expected but the UIView does not resize it in portrait mode.

While adding constraints to the UIView in storyboard for portrait mode, I have also added constraints for landscape mode.

I have also tried to add a variation for each constraint constant.

I am not able to figure out what the problem is. Any help will be appreciated. Thank you.

Upvotes: 1

Views: 901

Answers (1)

mother_chucker
mother_chucker

Reputation: 55

I had a similar problem with rotation of my UIView and I found the solution:

Transform the view based on the device orientation angle:

customView.transform = CGAffineTransform(rotationAngle: angle)

Adjust the frame of the view to fit the new orientation:

customView.frame = CGRect(x: 0, y: 0, width: containerView.bounds.width, height: containerView.bounds.height)

or:

customView.frame = containerView.bounds

Update the constraints and trigger a layout update to ensure that the view and its subviews are properly positioned and resized:

customView.translatesAutoresizingMaskIntoConstraints = true
customView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
customView.setNeedsLayout()
customView.layoutIfNeeded()

I hope this or something similar helps.

Upvotes: 0

Related Questions