Desgin ViewController with Storyboard - ios

I design the ViewController include 1 UIView and 1 button in it but when i deploy in the Emulatore, it 's not the same as what i see in storyboard file.

This is a consequence of the auto layout constraints you have. The default screen size in the storyboard is for iPhone 5, but the default simulator is an iPhone 4 size, so by having fixed distance to both the top and bottom of the screen, the view has to be squished to satisfy the constraints.
To fix this, you should give the view a fixed height (from the menu, Editor --> Pin --> height), and then delete bottom constraint.

You can also use distance to superview to top, also set the virw height constant. You dont need the bottom constraint

Related

Proportional width and height with main view in iOS

To make application compatible with all sizes of devices, At some point we need to give width or height of UIControl in proportion with main view that is our self.view.
So we used to assign constraint in storyboard, equal height/width to main view and then we set multiplier. Like 0.3 So it will set height of my control (iPhone 5s --> 568 ==> 568*0.3 = 170.4) and so on for all devices.
But right now I am creating app in Xcode 11.0. In that I cannot set equal height or width to main view.
I have tried by selecting my view and right click and drag to superview, but there is no any option.
Then I have tried with multiple selection. Select my view and superview and then tried to set constraint from bottom right corner, but no luck.
Until this is fixed in an update (presumably):
Ctrl-Drag from your UIControl to "Safe Area"
Select "Equal Widths"
Select the newly created constraint
In the Size Inspector pane, change Second Item from Safe Area.Width to Superview.Width
Although... you are probably better off leaving it at Safe Area

Top space on device rotation

I have the following constraints on my UIView in portrait mode:
And landscape:
This is screenshot of the ViewController when I open it first time:
Top space is OK.
Then I'm rotating the device to landscape:
Do you see that white space on the top?
And when I'm rotating it back to landscape, the space still stays:
Why and how to fix it?
Try to uncheck Adjust scrollview insets Of viewcontroller attribute
Source Of Image: Here
You might have set TopConstraint of your view With margin.
Uncheck box for Add to Margin.
Considering black view about the imageview which contains Back button is a UIView (says, viewNav)
Constraints for viewNav:
if you'r changing the height of viewNav of rotation then use size classes as see below and click on landscape option. Now select your navView and click on height constraint and give c-c constraint like this
Constraints for UiImageView:
If you'r image covers the complete screen the give bottom constraint as 0.
This fixes every thing.
If you are using native navigationBar then you there will be no viewNav and now constraint for image will be
On giving top constraint, don't forget to select use standard value for top constraint then enter 64. Bottom and height is according to your need whatever you prefer.

iOS view height and auto layout

I have a viewController, which is configured for the iPhone5 screen size. In red rectangle is the view.
To make this view show properly in iPhone4 - I used auto layout. But when I specify constrains such as: leading edge, trailing edge, bottom space and top space (in top space I even made constraint's priority to LOW ) - the view still goes partially down the screen as if my constraints don't work.
BUT if instead of top space constraint I specify view's heigh and delete top space constraint - everything works perfectly.
Does anyone can explain it please? Thank you.
Thats because when you set up the top space constraint it will move the view by the constant you provided. iPhone 4 and iPhone 5 screen height is different but the constant remains the same so obviously it will behave differently. One way to troubleshoot your interfaces is to switching between iPhone4 and iPhone 5 on storyboard device on storyboard (first button from left to right on the bottom right corner of interface builder).
Auto-layout is all about experience in my opinion. I struggled alot with it until i learned. If you want your view to be attached to the bottom of the screen you should set the BOTTOM SPACE to 0 and specify the view's height like you did or adjust constraints for it's subviews so that the height is set dinamically according to the views inside.

Auto layout constraints - Vertical spacing with one item of variable height

I have 2 UILabels above 2 UITextFields.
I would like the first UITextField to expand when the screen size allows it.
I set vertical spacings between each elements, and fixed height constraint for the UITextField at the bottom and a >= height constraint for the first one.
Below is a screenshot with what it looks like with the retina 3.5 form factor (<= iPhone 4).
But, it just doesn't work. Below is a screenshot of the storyboard with the retina 4 form factor (>= iPhone 5). The first UITextField overlaps the second one.
What constraints should I set ?
If i had it in front of me I'd play around with the compression resistance properties, which would be more elegant than fixed heights... however....a crude way of doing it that would work is:
You could have fixed height labels.
Fixed height bottom text view.
Pin the top text view to the bottom of the label. Pin the top label to the top of view.
Pin the bottom text view to the bottom of the view, pin it's label above it.
Pin the bottom of the top text view to the label below it.
The orange (-78) is Xcode's way of telling you you have a conflict between the frames of your layout and the autolayout constraints. Try looking at the storyboard warnings and fix the frame or fix the layout constraint.

iOS Scroll View, Container View - auto layout issue

I'm using storyboards & auto layout. I have a Container View in a UIScrollView. The Container View allows me to layout a long (320, 1000) view in the storyboard. I set the content size of the UIScrollView to (320,1000).
I pinned the following constraints on the ContainerView to the ScrollView:
Width Equals: 320
Height Equals: 1,000
Top Space to: Superview
Align Center X to: Superview
Xcode insists on adding an additional Constraint. It adds a Bottom Space to: Superview Equals: -432. It will not let me delete this constraint! I'm not sure if this affects the issue below.
Everything works fine on the 4" screen of my iPhone 5 and Simulator.
My problem comes when I test it on the 3.5" screen in the Simulator & my iPhone4S. The scrolling content size becomes restricted to (320, 568), losing the bottom section of my ContainerView.
Can someone help me understand why this doesn't work on the 3.5" screens?
Received the answer from Apple Tech support. It still gives me an error in Xcode, but it functions on 3.5" screens now. Here's the answer:
I'm not sure why interface builder decided to relate the bottom of the container view to the bottom of the scroll view's content view minus 432. You can not remove this constraint because it is necessary for the layout to be fully defined [1]. However, if you promote that constraint to a user constraint, you can edit the constant value to zero. This will correct the issue where the bottom part of your content was being cutoff.
In Interface Builder:
1) Select the Container View
2) Switch to the Size inspector
3) Click the gear icon next to the "Bottom Space to: Superview; Equals: -432.0" constraint.
4) Choose Select and Edit from the menu.
5) Enter a value of zero in the constant field.
[1]: See the In Depth Explanation section of Technical Note 2154 for more information. https://developer.apple.com/library/ios/#technotes/tn2154/_index.html#//apple_ref/doc/uid/DTS40013309
When you embed a view into a UIScrollView in InterfaceBuilder, then there's a constraint automatically set. If your view is "longer" than the screen in portrait, it wont scroll at all.
But if you rotate the screen, you'll notice the ability to scroll a little.
To get UIScrollView going in AutoLayout look into your constraints.
Find "Vertical Space - Scroll View - View" and set it from "constant" to "auto" - fixed everything for me.

Resources