Xcode constraint fix on landscape orientation - ios

I have a button at the bottom of my view which is constrained as intended on portrait, but when I rotate in simulator, it obviously retains the constraint and shows up before the rest of the content, which is supposed to precede it. I simply don't know how to keep it at the bottom of the view in both orientations.
Bonus question: how to scroll down in simulator?

Related

iOS Storyboard Constraints/Traits for portrait and landscape view

my iOS project consists at the moment of one single UIView or UIViewController. The controller class is called MainViewController.swift.
The view of the view controller has a subview which I've placed at the bottom:
I've set the auto layout option to keep the distance of the subview for every device size the same:
Now I want to keep the sub view always on the short side of the parent view, which means that in landscape mode the sub view show be on the right side of the parent view:
I've checked some tutorials and other posts in the meanwhile:
Ray Wenderlich Tutorial
Stackoverflow Post
At this point it is not clear to my how to realize such a behavior best?
Possibilites I've found to solve the issue:
Vary for traits...but how?
Constraints?
Programmatically - I think this would be the fastest solution, but I really want to use storyboards and learn the correct usage.
Here you can play with size classes and traits. if you want give constraint in portrait mode you have to select compact width and regular height and for landscape compact width compact height.
Please go through following steps to constraint portrait and landscape separately.
1) Please select mode either landscape and portrait from traits.
2) Now, you can constraint each mode one by one. suppose i am giving constraint to red view in portrait mode and i will enable particular constraints for for portrait size class so it will be disable for landscape mode.
3)once we are finished with portrait mode i have switched to compact height compact regular size class which is landscape mode.
4) Now, we can constraint the landscape mode as shown in above screenshot.
As we can see constraints which is light in color are disable for current size class.
So this is how we can constraints landscape and portrait mode separately.
Because there is no size class or trait difference between an iPad in landscape, and an iPad in portrait, you will ultimately have to handle at least some cases programatically. That being the case, you might as well just handle all cases programatically with a method that does something like:
Get current screen size
If width > height, add constraints to center the subview vertical and pin it to the right edge. Otherwise, add constraints to center the subview horizontally and pin it to the bottom edge.

Switching to landscape, auto constraints in wrong corner

I'm presenting a view controller when my view controller is rotated to horizontal.
In my NIb, I have a help button with constraints to margin top and right of 8 pixels each. However, my view controller displays with the button in the top right of a Portait perspective. See image. How do I make this show up in top right in Landscape?
It looks like your screen did not rotate, because the status bar is still in portrait mode. BUT... even if it did rotate, you would probably have problems because the status bar is 8 pts wide, and it is automatically removed in landscape mode.
I'd say (1) set your constraints to put the button 30 or 40 pts from the corner, until you get the rotation stuff worked out, and (2) fix the rotation issues by looking at the settings for which rotations the simulator supports. Normally rotations allowed by xcode (at least for my default swift projects) are left, portrait, landscape, but not upside down.
It happens to me all the time, when using the simulator!
(Assuming that your project is supporting both landscape and portrait mode)
To make sure your device has rotated from landscape to portrait or the other way around, simply rotate it three times instead of once! (Press the ⌘ and then → three times)
Also for your button placement, I would recommend creating a Standard offset to superview's top and trailing margins, without using any particular numbers (Standard pops up when you delete the number in the constraint's Attribute Inspector).

UIScrollView disappears when adding constraints

I have a UIScrollView that contains everything else in my ViewController. I use it because I have logic to scroll up the view when the keyboard shows.
I'm working to make my view compatible with variable screen sizes to accommodate newer iPhones. Currently there are no constraints on anything in the ViewController. If I open the app on a device, it appears in the top left corner.
The first thing I tried was pinning the left/right sides of the UIScrollView to the edges of the container, with left/right distances of 0. When I fired it up on a device, the view had disappeared entirely.
I then tried adding a Horizontal center in container constraint the scrollview (removing the other constraints). Again, just a blank screen - everything had disappeared.
What am I doing wrong? How do I make my scrollview fill the screen, or at least be centered on wider devices?
Is there any way to debug layout issues like this? At the moment, I have no insight into what has happened to the view.
Adding height/width constraints, even though these constraints were overridden to stretch the view to the viewport, fixed this problem.

Where can you specify a view's frame/location for specific orientations on iOS?

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:
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:
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?
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.

Part of the screen disabled in landscape mode on view-based iPad app

I am developing an iPad application with view-based template in landscape mode as it has to share a toolbar to all its views and provide the functionality similar to tabbar.
The problem is that any control added to a portion of the screen on the right side is disabled. For example, if a button is added, the part of it on that portion of the screen doesn't work.
Surprisingly, the width of that portion of the screen is equal to the width of the screen in landscape subtracted by the width of the screen in portrait so I think the problem has something to do with that.
Thanks in advance
I'm betting those controls are outside the bounds of their superview (or the superview's superview, or the super-super-superview, or…). When the view hierarchy does a hit test, it returns nil if the point is outside its frame, so subviews outside the frame can't be hit. Note that views in IB don't have the "clips subviews" option on by default, so it's hard to tell where the view bounds are. Also check the autoresize settings on those views--if one of the containing views isn't set to resize horizontally and it's sized to portrait width in the nib, it won't expand to landscape width when you rotate the device.

Resources