Position and size are ambiguous for “view” - ios

I have set pin to all view but i got continuously this alert !
I have seen that this is because of height and width constraints but i didn't add height and width still i got this error !
This alert should be avoidable or not.

A UIView has no intrinsic size meaning none of your views know how large they should be and thus cannot be laid out the way you have them. You can set the height of each one to a fixed number but if you want them to scale I recommend using Equal Heights and Equal Widths and set the multiplier to the proportion of the view you want the little views to size to be. You could do this so many ways.
I am going walk you through one way that I think is the quickest(using StackViews) for my own sanity but the same logic could work with more dragging from each view.
Step 1:
Drag a Vertical StackView to the top of the storyboard. Add the following constraints. pin leading, trailing, and top =20. Then drag from the vertical stack view to the main view and choose equal heights and change the multiplier to 0.6(60% of the main view height). See image
Step 2:
Add a single horizontal stackview as an arranged Subview to our vertical stack view. Now add 3 UIViews and change the colors to desired colors. Change stack view to Alignment-Fill and Distribution-Fill Proportionately. Also add spacing=20. See Image
Step 3:
Choose your horizontal stackview that you just created in step 2 and hit Command-C to copy it. Hit Command-V to paste it. Note- If it does not paste it into the vertical stackview drag it in to the vertical stack view. Not there yet but close. See image
Step 4: Go to the vertical stack view and change it to Alignment-Fill and Distribution-(Fill Equally). Add Spacing of 20. You should now be a point that looks like this. See image
Step 5. Add a horizontal stack view below the vertical stack view and the bottom layout guide. Pin to all four sides at 20. Add three views and change the color to desired color. Change Alignment-Fill, Distribution-Fill Equally, and spacing=20.
Step 6. Adjust verticalStackView equal heights multiplier to maybe a lower number(0.5) to make it look like your view.
Step 7. Reap the rewards
The Big takeaway is that a UIView needs to know how big it is. UIStackView in this case tells the views how big they are. You could just have easily set the height and width of one of your views in your screenshot above to a percentage of the view. Then drag from that view to all of the like sized views and set equal heights/widths. That would have been more tedious and you can see why I used Stackviews for the example. Good luck.

Related

How to use auto layout to resize views in a table view cell?

I have a cell in which I place four buttons and four labels. Each button gets assigned a picture with width 50 and height 50. Furthermore, all buttons have a corresponding label describing what they're intended for.
My objective is to have the buttons and labels resize to keep the buttons' and labels' aspect ration intact while the screen dimension changes on different devices. I have been playing with auto layout changing the hugging and compression to achieve this but haven't been successful yet. Any help would be much appreciated...
I think you should take a look at a UIStackView, because this seems exactly as a use case for stack. Just put each pair button/label in a stack, and then all four pairs into a horizontal stack, which you constraint to the cell itself. You should be able to handle all you need just by configuring the stack’s properties (axis, distribution, alignment, spacing).
Embed your button and label into a view. Set the width of this view equal widths to content view and change the multiplier value to 1:4. This will adjust the widths of the views according to superview. Also, set the top and bottom constraint to 0 for this view.
Provide center align y-axis constraint to button after setting the width and height constraint to 50. Set its top constraint to a value you deem fit.
Set labels's leading and trailing constraint to a value like 8. Choose center alignment for text. Also, provide top constraint to buttona nd bottom to its superview.
Copy the view and paste to create the three views and provide them equal widths constraint to the first view. Also, provide their leading, trailing, top and bottom constraints.
Here are a fast tutorial in how to achieve that:
1-
2- completion of the first Gif:
Note you can achieve the same output using a UIStackView

making an auto layout of UIimageView and textview inside scrollview for iOS 10.2 in xcode8

I am making a note taking app in xcode8 for iOS 10.2. The note is consist of a textfield, textview and imageview. I am adding textview and imageview in separate scrollviews (to provide the facility of zooming and adjusting image). Then I have added both scroll views in a stackview to ensure that imageview and textview stands side by side in a landscape mode. To do that, I have added variation to axis property of stackview( width=any, height= compact and Gamut=any, and set that to the horizontal, as shown in right corner of screenshot to see view hierarchy without any constraints).
I have tried many constraints but, every time it fails to adopt. Let's take an example, I am adjusting size of the scrollviews on the size of content(by setting top,bottom,leading and trailing constraint), the stack view is adjusted to the main view. Now, the only thing that needs to be adjusted is text view and imageview with respect of stack view. But, here is the tricky part If I include a fixed trailing constraint of text view to the stack view; it will work in portrait mode.As soon I turn into landscape mode that constraint will not work(the trailing constraint which have 0 distance from stackview) because the edge will be side to the imageview not to the stackview(because I am putting image and text side by side as explained above) which will make textview larger than required, same goes for imageview's leading constraint to the stack view and vertical distance constraint betweem image view and textview.I think the root of the problem is I have made a variation to the axis of a stack view (in order to get text and image side by side in landscape mode) but, that was necessary part.Can anyone have solution for this? Do you think there can be other problem? Should I choose Intrinsic Size of scrollviews to placeholder option or let it be system default?
Thank you.
So I hope I understand the question and I will try to walk you through what I understand you hope to accomplish. I preface this by saying I just started using UIStackView because of backwards compatibility.
Note. To get the magic of ScrollView with AutoLayout I almost always embed them in another UIView. There are reasons to not but in this case you will see how valuable this is to AutoLayout.
Step 1) Drag your UIStackView and add Top,Bottom,Leading, and Trailing. Now add 2 UIViews and set the UIStackView to Fill Equally. It will now look like the image(Background colors to check your work).
Step 2) Now add a UIScrollView to each of these UIViews. Add Leading,Trailing,Top,and Bottom on each of these.
Step 3) Add a UITextView and UIImageView to the ScrollViews respectively. Now Autolayout is mad at us :( but we will fix that.
Step 4) Drag from the UIImageView to the View that is holding the UIScrollView(First Set of Views we added). See image
-Choose the option to add Equal Widths. See Image
Repeat the same step but add Equal Heights. Now add Top, Bottom, Leading, and Trailing to the ScrollView.
Step 5) Repeat exact Step 4 with UITextView
Your final view hierarchy should look like this.
Now you can rotate your UIStackView and do what you want I think.
And Horizontal
Side by Side Preview
I did add a <= 0.1 equal heights multiplier on the textfield at the top but I don't know if that was necessary.
Enjoy.

How to set space between two view using auto layout(in Aspect Ratio)

I have two views in a view and i have a requirement to increase the distance between those views in aspect ratio to the main view. I am using auto layout.
Thanks in advance.
One way to solve your problem is -
For your upper view give top space constraint with super view and other required constraint.
For your lower view give bottom space constraint with super view and other required constraints.
You don't really need to give vertical space because your views will be pinned with fixed space with top and bottom of super view, so if screen size will grow space between your views will automatically grow.
Important to note however: You must add the height constraint for both views and have either fixed width constraint or have Leading and trailing space constraints.
See ScreenShot
Since you have more than one view, I recommend doing the following assuming this is the result you are looking for:
STEP 1 :
select all your views
Step 2 :
go to Editor -> Embed In -> Stack View
(now it will group them together) I'm assuming that you set the width and height constraint for each of your views before doing this.
Step 3 :
once you have done that select the stack (not the individual views). but rather the entire stack (I would do this from the side menu) and set the top layout and the bottom layout.
step 4 :
Go here and make these settings for the stack as you have it selected
and that should do it.
Let me know if I should clarify anything.
UPDATE:
If you do not want equal spacing, you can still use the above method by playing around with different stacks, etc.. However, There is a method I've used in the past to get this, which is setting a multiplier instead of a number for say spacing between bottom layout and view. Here is an example that might help below:
you can play around with it to get your desired results.

iOS Autolayout: Handling different screen heights

I am desperatly trying to get to grips with Autolayout and need advice on how best to approach handling the following scenario. I am working with an existing design that I need to lay out appropriately so it will function on any iPhone screen size.
For each screen, I have identified a section of the app that should partially expand/collapse to compensate for the difference in screen height. I have illustrated this in the image below. Primarily, is this the best way to approach the problem?
Assuming this is the best way to approach the problem, how should constraints be added to fix the positioning and heights of views 1,2 & 4, while allowing view 2 and it's content to flex in accordance with the device view height?
I have experimented profusely with constraits and have been unable to achieve the illustrated outcome.
Here is how I would have done it.
All views:
Align the leading and trailing space to the superview
View 1:
Align the top to the superview or top layout guide
Add a fixed height
View 4:
Align the bottom to the superview or bottom layout guide
Add a fixed height
View 3:
Align the bottom to view 4 (using vertical spacing)
Add a fixed height
View 2:
Align the top to view 1
Align the bottom to view 3
If you want the result in your pictures, here is how to do it. You can choose to use wAny|hAny size class.
From top to bottom, set vertical spacing between each adjacent views to be 0.
Set height constraint for view1 and view3 and view4 to be a static value.
Then the height of view2 should vary on different screen sizes.
Looking from your picture, your view1 occupies almost half of the screen space. So my suggestion is to make its height to be proportional, you can set view1 and its superview to be equal width, then modify this equal width constraint's ratio to be 0.5. You can do the same for other views in your picture.
In addition to other answers, with the visual format, it should look like smth like this
V:|[view1(HIGHTV1)][view2][HIGHT3(HIGHTV3)][view4(HIGHTV4)]|
Repalce HIGHTVX with the static sizes

Is it possible to set UIView's top relative to other UIView's height using AutoLayout?

Is it possible to set UIView's top relative to other UIView's height using AutoLayout? In other words, I want to give a view different top values depending on height of screen size.
For example, I want a result of third one not second on the following image.
To achieve the result you are aiming, you can use a little "hack", adding three accessory UIView (with clear background):
View #1 above the blue UIView, with a constraint = 0 to Top Layout Guide
View #2 between the blue and purple views
View #3 below the purple UIView, with a constraint = 0 to Bottom Layout Guide
Next, you need to add two other constraints, Equal Widths between Views #1 and #2, and between Views #2 and #3
Finally, you need to add Vertical Spacing constraints (= 0) between all the views, linking each view with the one below (or over). The key here is to lock those views between them, so that the vertical layout is not ambigous.
Now you can just add a specific height constraint to blue & purple view and they will stay in place, floating if screen size changes.
Note #1: Be sure to constraint these accessory views to the Main ViewController view, in order to center them & fit them to parent width
Note #2: If you don't know their height before, this solution will not work. You could try with some constraint set to Greater Than while playing with priorities but I didn't test it.
Interface Builder screenshot

Resources