iOS Storyboard: Different Layouts for Different Devices - ios

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.

Related

What must I do to add a constraint to a view in a Storyboard launch file?

I added a Storyboard launch file to upgrade a project from iOS 4 to iOS 9. The app runs in the Simulator but nothing I do will centre views for iPhone5 and iPhone 6 or scale them to fill the frame. Instead they look like bonsai versions of an iPhone 4 pinned in the top left of the screen.
When I try to add constraints to centre the views by following the procedure described here, the menu will not give me options to choose the view I need to constrain.
Instead I get this.
The bottom two - Horizontally in Container and Vertically in Container - are the only options I can check. But pulling down on the arrow to the right of the text box only gives some of the options needed to Add Constraints
What do I need to do to enable the view option ?
EDIT 1.
And furthermore, when I try to constrain width and height I get this
All six options are in grey and none of the text boxes change
EDIT 2.
And using the Size Inspector to look at the view I am trying to centre and fill, the view has no constraints as shown below.
It is really starting to look like there is no simple way to launch an old app where views have been created programmatically and have Storyboard Launch automatically layout views to suit different iPhone screen sizes.
The "Horizontally in Container" and "Vertically in Container" checkboxes affect individual views. The are "binary operations". If you select more than one view, these checkboxes cause all the views to be centered in their container.
In contrast, the other checkboxes in this popup align groups of views.
If, for example, you select 4 views and click "leading edges" then it creates a set of constraints that line up the leading edges of all the selected views. (Under the covers it creates enough pairs of constraints to line up all the views by their leading edges.)
If you only have 1 view selected then only the bottom 2 checkboxes ("Horizontally in Container" and "Vertically in Container") are meaningful and the others are dimmed. As soon as you select more than one view the others should be enabled.
I had the same problem because on Show the Size inspector > Layout, "Autoresizing Mask" was selected, by changing the value for "inferred (Constrains)" it will resolve this problem
illustration
Select the view you would like to constrain. Then tap the button to the right of the one you have selected and use that menu to define your autolayout constraints. If you want it to work for all devices make sure your storyboard is on the wAny hAny size class setting.

UIImageView sizing in AutoLayout + Size classes for all devices

I'm working on app and will for 1st time use auto layout/size classes. Today my first day learning it and I understand constraints now and how to place the images where I want them to be on every devices from iPhone 4s to iPad PRO.
As my main storyboard in Attributes inspector i'm using INFERRED size. In show Assistant Editor, Preview, i'm using all device sizes.
In my inferred storyboard i'm using a UIImageView size 170 Width, 230 Height. Then I use AutoLayout to make it center of every device screen in preview.
Now the issue is this UIImageView covers HALF of the size of iPhone 4s preview but only about 1/5th the size of iPad PRO. How do I alter the size of each UIImageView to the size that I want them to be in each device without changing that centered position of each UIImageView in the preview devices?
You need to first set up your constraints properly and then choose the type of scaling you want.
Set up your constraints:
Click on the Pin icon |-[]-| at the bottom of the screen. Turn on all four orange I-beams. Set the four constants to zero.
Uncheck the Constrain to margins checkbox and click 'add 4 constraints'
Then set your scaling:
Click on the Image View you added to your View Controller.
In the Attributes Inspector on the right, choose your image.
Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncrossed).
That should work
Maybe you can use a pending or proportion constraints instead of width and height constraints.

Xcode 6: How to make an image view fill the screen on all devices? Auto layout not working?

So I have an image view and a button. I need the image view to be the correct size (not look squished or stretched) on all devices and more importantly I need it to be the size of the screen. I need the button to stay in about the same place on all devices.
Back with Xcode 5 I remember auto layout fixing this problem but now that does not work. I looked at https://www.youtube.com/watch?v=G53PuA_TlXk and tried control-dragging to the view controller and centering the view both horizontally and vertically, but I can't get it to scale to the size of the screen. The video doesn't address this.
Is there a specific option I need to choose when control-dragging to keep the image view scaled? What should I do?
You need to do 2 things:
Set up your constraints properly
Choose the type of scaling you want.
Setting Up Your Constraints:
Drag out an Image View and place it roughly in the center of your view.
Set your size class to wAny hAny.
Click on the Pin icon |-[]-| at the bottom of the screen.
Turn on all four orange I-beams. Set the four constants to zero.
Uncheck the Constrain to margins checkbox.
Click Add 4 constraints
Choosing Your Scaling:
Click on the Image View you added to your View Controller.
In the Attributes Inspector on the right, choose your image.
Set the Mode of the View to Aspect Fill (fill entire screen cropping top/bottom or left/right as necessary) or Aspect Fit (letter box image so that entire image is shown uncropped).
You can view the constraints that have been created for you. In the document outline, you should see 4 constraints under your image view:
Click on a constraint and then view them in the Attributes Inspector on the right. Here are the 4 that I created:
This is what worked for me:
View Mode:
Aspect Fill
As constraints for the image:
Align Center X to: Superview
Equal width to: Superview
The 'Equal width' property is the one that scale the image to the size of the screen.
If you want to have the button in a specific position, set constraints from it to the view, the main ones you want to consider are a top constraint and any left or right constraints. So you are telling the view where you want to position the button in relation to its sides.
Hope this helps.
Not sure I understand "exactly" what you're after but if you want to drag an image view into the controller, and that image view to automatically fill the whole view controller background: just drag the image view component into the view controller window.
Thing is: if you see a blue bounds line around the view controller, before you drag, the image view won't fill the view controller.
You need to click anywhere on the background in the view controller pane, so the blue bounds line disappears, only then, will the image view you're dragging in, fill the whole controller background automatically.
If you find, using previews, your pics are getting squished or stretched, then, just go to the "Resolve Auto Layout Issues" menu, and click "Add missing constraints" - then all should resize to look ok on all screens.
In my constraints, I needed to uncheck "relative to margin".

Auto layout: Xcode 6: Centering UI elements

I'm using Interface Builder in Xcode 6 to make an app and am having trouble getting the text fields and button to centre on the screen for different size screens.
I thought it was a matter of selecting horizontal and vertical centering in container but it doesn't seem to be that when I try it in auto layout. Actually I've tinkered around a bit and I still haven't got it.
I just want to be able to see all of my button and text fields for any size iPhone screen and right now simulator is only showing part of these UI elements like this:
I also want to do this in storyboard and not in code as I'm not at the level of doing this in code yet.
Step 1: Make sure your size class covers all the iPhone screen at least in portrait view. So, change the size class to "wCompact hRegular".
Step 2: After setting the size class properly, add the UITextFields and UIButton to your storyboard. To me, it looks something like-
Step 3:
Before, you start adding constraints, you need to remember two things-
a. Your element(UITextField, UIButton, UIView or any component) needs to know its starting position unambiguously, and
b. Your element needs to know its size meaning, its height and width.
In this case, as you want to centre your elements, I am just assuming that it needs to be centred starting from 10 scale from the left edge and should end 10 scales away from the right edge of your iPhone screen. Now, that means, it's width will be different based on the screen size, but its height will be same.
So, I just add the constrains following way for the 1st text box-
Notice, in the size inspector, I set the text box's starting point, x and width in a way that it is 10 pt away from left edge and 10 pt away from the right edge. Don't worry, it's just simple math.
For the 2nd textField, I add the constrain, the same way-
Lastly, for the button, the constrains are following-
Now, you are good to go. Everything is centered.
By using your size class selector in the bottom of the storyboard window, set you sizes as any width and any height and then follow the below auto layout constrains. It will work for you.
First select the view you want to set the auto layout, and then select the pin option from the right bottom corner of your storyboard and then add the constrains as shown in above picture and click button Add 4 constrains
Repeat the process for all views and set the constrains as Fix the top, bottom, left and right constrains of all views except the last button that should be fix from top,left,right and fixed height.
You need to make use of the size class selector in the bottom of the storyboard window.
So for an iPhone 6 or 6 Plus in portrait you would choose a compact width and regular height like this:
And then you would do whatever auto layout stuff for the given device there

Change Text Alignment in UITableViewCell On Rotate

I am using sizing classes to turn two xib files (iPhone vs iPad) into one. Most orientations have a 2x2 grid of UILabels above a UITextView. However, for iPhone portrait, I want the UILabels to stack vertically. I have this working great with sizing classes. I was disappointed to see, however, that sizing classes don't allow for the change of text alignment. Refer to my diagrams below. In the main orientation I have the text in the yellow and blue UILabels right-aligned. In the iPhone portrait orientation I want the same UILabels to have left-aligned text. How can I change this if the user rotates the device while viewing these table cells? I figure there's a better way of doing this than reloading the table.
I dont think it is possible to setup the text alignment based on the screen orientation or device. however you can create 2 labels, one with left aligned text and the other with whatever you're using otherwise, in a way that they are exclusive to the screen layout you're using. I.e. one label for iPhone and one label for everything else.
You can select then, for that UILabel object in storyboard, which screen layouts it will be installed for.

Resources