How to set constraints based on device - ios

I'm trying to display a button as per the image below. It's width and height are determined by a XIB that this button is a subview of (that's not really important information for this issue though).
The constraints are setup for the "SkipBreak" button in Interface Builder.
You can't tell from the image I've attached, but there is a transport bar located beneath the button. The alignment of the button relative to the transport bar is fine if I test on an iPad (in either portrait or landscape orientation). But if I test on an iPhone (such as the XR model) in landscape mode, the button ends up overlapping the transport bar.
Is there is a way I can set the constraints of this button so that the anchor point from the leading and bottom edges of the superview differ based on the device?
Is this what size classes are for? If so, is there a resource you can point me to in order to figure out how to do this?
Otherwise, do I have to delete the constraints programmatically, check the device type and then add the constraints?

Related

Alignment issues in iPhone X

In my application i have a screen which contains close button on top and program information at bottom and both trailing,leading,bottom and top constraints related to safe area.
Button width and height is 1:1. Program info area height is 0.25 of view height. In run time adding avplayerviewcontroller view on screen whose height is 75% of self.view.It's working fine in all device except iPhone X. If i launch app in landscape then alignment is fine and if I change orientation I can see 44 pixels as x value. If I launch app in portrait first then portrait orientation values are correct if I change orientation to landscape then 44 pixels top(y) value is adding. I am not getting how to fix this issue. Please guide me.
[![enter image description here][1]][1]
I guess your problem is that you have defined constraints in percentages, which might be an issue. When you rotate the screen the mid section might be too small. Have you tried the rotated layout in interface builder to see what is happening? Defining layouts with constraints is relatively easy in interface builder and you can test the layouts as well.
If you programmatically set the constraints, it is hard to find out where it went wrong. If you have one ambiguous constraint, the whole screen can be weird.
If you need to make programmatically set constraints, you should make a test setup in interface builder and implement the good setting in the program code, when everything works as planned.
I have checked your storyboard and I think the issue might be that you have two safe area defined. One outside the containerview and one inside. What you put inside the container view, might be wrongly calculating the constraints inside.
Try to take out this checkbox and try it again.

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.

iOS Storyboard: Different Layouts for Different Devices

As you can see, this is the different device previews for my current storyboard. The transparent green on the bottom is taken up by another view controller, but the spacing needs to be fixed for each different size class. I tried setting the width and height in the storyboard to the setting I want to change, but it changes that attribute for all the size classes. How can I just change, for example, the size of the Label at the top for ONLY 3.5in iPhones or the green layout for ONLY 5.5in iPhones?
Choose the pin of the label and go to Attributes Inspector of the Utility Area. You can find + button left to the constant text field.
When you tap the button, you can choose the size class you need.

constraints doesnt keep it in the middle

When I add an image and constraint it to remain in the top-half center of the screen; I would like it to work with all iPhones except it doesnt,.the problem is it only works with iPhone 4 portrait and landscape that I have but when I change the iPhone to iPhone 6 it doest stay in the middle, does anyone know why?
To geta an element in the middle of the screen here are a few tips:
Ensure you drag the object in to the middle of the screen (Horizontally). You will notice guideline's appear up the screen.
Set the constraints on the item which include 'Set Horizontally in container' & Set a top constraint only (You may want a height wand width also, experiment with this.
For an image make sure that you have set your fill property for your application. (Aspect Fill etc)
A good tip to get used to auto layout is to set your view controller to iPhone 4 and design on that (With constraints of course)
Auto-Constraints are rubbish, create them yourself!

How do I get a textView to expand when screen is rotated from portrait to landscape in iOS

I am using the Interface Builder to create this UI:
And when it is in portrait the One and Two TextViews fill the width of the screen (which is what I want), but when I rotate it to landscape mode the TextViews do not take up the full width of the screen.
How can I get the TextViews to fill the width of the screen in landscape mode? In the Interface Builder, the "Autolayout" option is checked and if possible I would like to accomplish this with it still checked since everything else works best with it on. Any advice would be greatly appreciated since I am still fairly new to iOS programming.
Now the screenshots are posted below. If you notice text box One for example goes all the way to the edge in Portrait view, but not in Landscape view.
If you dont want to use AutoLayout, You can use following steps.
1) In your XIB uncheck autoLayout check box.
2) Select your TextField and set autoResizeConstraints as follows.
Tyr this .... It will help you....
You can either change the textView size programmatically when t change to landscape in the method
-(void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
Select your UITextView and press the "Pin" button.
Click the left and right constraints so they turn from dotted light red to red.
Enter your required distance from the superview. In my example, 5 pixels.
Remember, these two constraints are not enough to define a position of the UITextView.
To do this you must either:
Pin the view to either the top or bottom and provide a height
constraint.
Pin the view to both the top or bottom
Center the view vertically in the container
Align the view with some other subview that has a resolvable Y
position

Resources