iOS Auto layout problems with spacing - ios

I'm trying to place UIImage, UITextView and UIView one after another vertically.
I've set all constraints and spaces between views. But it says that it need constraints for Y position or height for UIView and UITextView.
What have I done wrong?
Here is screenshot: http://s019.radikal.ru/i616/1509/63/f18ed2a349f3.png

Generally these problems arise from putting a UIImageView itself with constraints. Your best bet is to put in a view and embed the image inside the view. This should make your life a lot easier. Just reset constraints and then click add missing constraints on the whole controller. :-)

Related

UICollectionView Disappears In UIStackView

I have two UICollectionView and two UILabel.
Each UICollectionView and UILabel are put into a UIStackView (distribution set to fill, and the height of the UICollectionView set to 75). Then, the two UIStackViews are placed into another UIStackView. This mainStackView is centred to the x and y of the view.
My issue is is that when I run the app, my the UICollectionViews disappears, and I have no idea why.
This is what my viewController looks like in the storyboard
And this is how it looks when running:
Any help would be appreciated.
Make sure to set the constraint Anchors (if you're using Anchors) of the UICollectionView to leading, trailing, and the container UIStackView needs those as well as topAnchor, bottomAnchor. Also make sure that everything either has a height set, or has intrinsic content size.
I'll tell you what though - 75% of the time when I thought a UIStackView would make my life easier, it didn't, and added on to development time two-fold. You're using UIStackViews inside UIStackViews so expect extra complexity. The result you're trying to get could also be achieved via regular old Anchor constraints and could be much simpler. Throw it together in Playgrounds and see what I mean. I'd be happy to help if you need it with that.
If you still have issues and if you still want to go the UIStackView route, please update your question with code or indicators around constraints. Good luck!

What are the correct constraints on UIScrollView from Interface Builder? Pure Autolayout from IB

I have been trying to get ScrollView to work for 2 days now, and it doesn't work at all. Most of the suggestions here on SO and other websites say that you need to pin the ScrollView to the root view and then place a ContentView (UIView) inside ScrollView and then pin it to all sides of the ScrollView (so that the scroll size can determine the contentSize... However this does nothing). There's also conflicting information out there, one video says that there needs to be a constraint from the bottom of the ScrollView to the ContentView. Neither solution has worked for me. Here is what I've been doing in most of the combinations I've tried:
UIView -> UIScrollView
Pin all sides of the UIScrollView to the UIView
Create a UIView (name it content view) and place it inside UIScrollView
Pin all sides of the UIView to the UIScrollView
Problem at this point: UIScrollView needs constraints for X or width AND Y or width. The only thing that seems to solve the complaint is setting the UIView inside the scroll view centered horizontally and vertically, but this does nothing to make scrolling work. Another option is setting the UIView equal height and width to scroll view, but again, that does nothing other than remove the complaint.
I don't understand. Isn't pinning the sides, setting the constraints? IB seems to think that this is not the case.
What are the correct constraints needed? All I need is a simple view with stacked controls (to fill out a form) and the screen needs to be able to scroll if the form is longer than the screen.
I'm using iOS for the first time, and building purely from IB for now... minimal code solution would be best.
You are half way there. First you need to decide what you are going to display in the scrollview, you have placed a content view, that needs to have an intrinsic size. You can choose to put there static or dynamic views. Static views will have their size defined at design time, and that will resolve the UIScrollView AutoLayout constraints. If instead you are doing it at runtime with dynamic views you will need to choose a default size for your content view, create an IBOutlet for the width and/or height of your views and then resize them at runtime altering the outlet in viewDidLayoutSubviews. The video you linked explains that quite clearly.

Adjusting UIView Height Inside UIStackView Control

I am using a UIStackView in iOS 9 SDK. The height of the stackview is 44 points. I have a UILabel and UIView inside the StackView as shown below:
Now, I want to make the green view much smaller than 44.. much like 20. How can I do that?
Without the UIStackView I am getting breaking constraints:
[GT.BubbleView:0x14536610] (Names: '|':UITableViewCellContentView:0x14536fd0 )>",
"<NSLayoutConstraint:0x146b7300 GT.BubbleView:0x14536610.trailing == UITableViewCellContentView:0x14536fd0.trailingMargin>",
"<NSLayoutConstraint:0x146c4000 'UIView-Encapsulated-Layout-Width' H:[UITableViewCellContentView:0x14536fd0(286)]>")
First you need to add UIStackView on the cell
Need to add constraints to your UIStackView
Adjust your UIStackView
Drag&Drop UILabel into your UIStackView
Then Drag&Drop UIView into your UIStackView
Add green-UIView like subview to pink UIView
Now you can add couple constraints to green-UIView
Now you can run app on simulator or device
Notice that I don't added any constraints for UILabel.
If you are trying to achieve a 20x20 square like the one below:
Then all you have to do is add another UIView to your green view. Make that UIView's size 20x20pts. Then change the small view background to green and the larger view's background to clear.
That said, this solution doesn't require a stackview and possibly over-complicates your UI. I would recommend just using autolayout instead.
Stackviews are most useful when
you want to compensate for a change in device orientation,
want to stack a lot of things on top of each other,
want to create a very complicated layout with many elements but that has
patterns you can repeat, or
simply want to be able to remove an element or add an element in with the sizing correcting itself
That list isn't exhaustive, but should give you an idea of when and why you'd use a stack view.
Outside of a prototype cell
This behavior is trivial. You can simply set the constraints on your view:
'
And then set the constraints on the stackview:
Hope this helps, cheers!
The breaking constraint problem can be easily fixed by setting the priority of width and height constraints to 800 or above.
I wanted to post my answer here so others could see, despite a slightly different problem from OP's.
I was trying to resize a UIView that contained a UIStackView and the constraints were breaking. I was changing the UIView's height constraint between 0 and 66 depending on if there was relevant info to show that the stack view was displaying for.
I was able to fix the breaking constraints by removing top and bottom constraints of the stack view inside the view, and instead set it to be vertically centered. This resulted in much smoother scrolling in my table view.

How To make Scrollable our Freedom View controller using constraints?

Actual image is.....
->View controller
->view
->ScrollView[[top,bottom,leading,trailing]to its super view]
->contentView[[top,bottom,leading,trailing,vertical,horizontal]to its scrollview].
that actual image is for iphone6 ->iphone4s,Any one help me how to do this.....
When it comes to set constraint with scrollview then little bit extraa effort is required for handling scrolling so that Scrollview + autolayout works.
First of All What exactly take place when autolayout is used :
When you use the auto layout, your views will resized automatically based on the constraint properties.
But, when you use a UIScrollView with auto layout like a container, the auto layout not works like expected.
so Here is Very good tutorial g8production which clears this confusion. Big Thanks goes to g8Production for providing this tutorial.
I think you are setting the constraints for content view in wrong way. You are trying to set the constraints on contentView that are related to its container(scrollview) which don't know content size of its own. It determines its content size from what it contains (in your case contentView,). Both are dependent to each other for their size. So both are confused.
To solve this you should first set the fixed height and width of contentView and then make outlets of these constraints and update these constraints with the height and width of superview when your view loads.
add top, bottom , leading and trailing space to your scroll view to view. It should be set accordingly, how you you want it to be seen. Then set same for content view (image). just addheight and width constraint for your image.
Misha :)

Resizing a UITextView in UIScrollView using Auto Layout issue

I really hate to ask here because I usually try to figure things out on my own. But on this one I've stuck for days and can't find a solution anywhere online.
I have a ScrollView containing multiple subviews. I've got an image view and two labels at the top with fixed heights. Then there is a UITextView and another ImageView (see pictures).
I add the text to the text view programmatically so it should have a dynamic height and the ImageView should move to the bottom so you can scroll. I don't want the TextView to be scrollable in itself but I want all the subviews to move as well.
I know I should be able to solve this issue using constraints. But I feel like I've tried everything and nothing worked yet. It worked when I disabled auto layout and moved the views manually. I'm wondering if there is a better way though.
As you can see I pinned the TextView to the ImageView above with a 1,000 priority and to the ImageView below with a 1,000 priority. The height constraint can not be deleted so I set it to the lowest possible priority. The ImageView on the bottom is pinned to the bottom of the superview with an absolute height. Its height constraint also has low priority. (I can post an image of the ImageView's constraints, if it helps)
I also tried adapting the frame programmatically but this is not working well in combination with auto layout. (If it helps I can of course post the code)
What am I doing wrong? Shall I just disable auto layout and do it manually? This seems unclean to me. Is it even possible to do?
I really appreciate your help :)
Greets,
Jan
Make sure the Scrolling Enabled attribute on the UITextView is unchecked in Interface Builder. I believe that the Auto Layout system takes that into account when it calculates the intrinsic content size.
If somebody is struggling with a similar problem: This is what I ended up doing:
Remove all subviews from the ScrollView in IB
Programmatically add a single UIView to the ScrollView.
Add all the views to the UIView as subviews (move them using setFrame)
Set the Frame of the UIView appropriately to the subviews
Set the ScrollView's contentSize to the size of the UIView.
A little more work but it finally works. This follows Apple's mixed approach guidelines that can be seen here (look for UIScrollView): http://developer.apple.com/library/ios/#releasenotes/General/RN-iOSSDK-6_0/index.html
The problem is the height setting. You somehow have to try to delete it. If you have added other constraints that are "sufficient", it should become deletable.
At the moment you have one user constraint for the height that is "Greater or equal" and an "Equals" constraint as well. Clearly, those are not working well together.
Maybe there is a conceptual error as well. The lower image view should not be fixed in position, so the distance to the lower image view will not be a "sufficient" constraint to let you delete the fixed height.
I think it should work if
the lower image view has a fixed height and
a fixed distance to the text view above, and
the text view has a minimum height as well as
a fixed distance to the image view above
(which should be fixed in relation to the superview).

Resources