Question

In an iPad app have a UITextView as a subview of UINavigationController that is set to take up a majority of the screen in portrait orientation. When the iPad is rotated to landscape, I would like the UINavigationController to stretch and adjust to the screen size, but the textview to keep the same size, but with it's position adjusted to be in the center. Here's a visual:

enter image description here

The textView is the same size as it was in portrait. Only difference is it was relocated to the center. The problem is I don't know how to do this.

First problem is that when I rotate the device, the textView takes up the entire grey area, the entire view. The textView was set up in interface builder in a storyboard and I made no setting or indication that I wanted this behavior. It just happens. This is not what I want. The textView is set up in interface builder, so the only checkmark I see is "Autoresize Subviews". I'm assuming this behavior is "autoresizing" so I uncheck that, and still nothing. I assume because it only applies to it's subviews. So I go to the UINavigationController and uncheck it's Navigation Bar's "Autoresize Subviews". Still nothing:

enter image description here

The only thing that kind of works is if I reset the textView's frame in the didRotateFromInterfaceOrientation method. Sure this "works", but this isn't what I want. Because first, once the device rotates to landscape, the textView still stretches. It's not until the rotation animation is over that the textView snaps into the size and position I want. And second, if the app starts up in landscape, the textView is still stretched. The resetting of the textView's frame only happens for a rotation.

Summary : How can I set the frame for a view for specific orientations?

Was it helpful?

Solution

You should set up the layout constraints in IB to make it do what you want. Make the text view whatever size you want it to be in portrait. Give it center x, center y, top space to superview, and a width constraint. If you have all these constraints (and no more), it should keep its same width when you rotate.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top