UICollectionView Disappears In UIStackView - ios

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!

Related

UIScrollView with multiple multi-line labels and AutoLayout?

Is there a way to achieve this? I have tried literally everything and nothing has worked for me yet.
So basically what i want to do is the following: I have a scroll view with some labels in it. All the labels get their text from a server and I have set their number of lines to 0 so that they change their height according to the amount of text. However, this does not affect the scrollview content size(even though my labels have constraints set up to the bottom,top,leading and trailing of the scrollview) and the labels go off screen and I am unable to scroll down. Can someone point me in the right direction to how I would set up my constraints, my view hierarchy and etc?
Any help is much appreciated! :)
Late, but this solved it for me:
Set leading (I have a 32pt inset), trailing and top constraints. The trailing will not actually seemingly do anything..
Make the trailing Greater Than or Equal to avoid localization alert.
Finally, add a new Equal Width constraint to the label matching the scrollview. Use the constant to subtract the required padding (I used 64 due to mirror my leading inset).
And voilĂ ! The Label will align correctly both in IB and in-app.
In Scrollview the last view's bottom constraint is so important. You should set its priority to 250 and put it to Greater than or equal.
Remember you should only change the bottom constraint of the last view, which in my case it's the continue button.
I would consider using UITableView instead, it has several benefits:
It allows for reuse of cells, if all the cells look the same
It manages recycling of cells when the number of values you're getting from the server increases (decreases memory pressure when number of cells becomes substantial)
It allows for more flexibility with the content (it's quite often for design to change last second or to evolve over the course of the project)
Most importantly, UITableView support auto sizing cells (as of iOS8), you need to specify the constraints between the label and the borders of the cell
There are several resources to start with:
http://www.raywenderlich.com/73602/dynamic-table-view-cell-height-auto-layout
https://www.captechconsulting.com/blogs/ios-8-tutorial-series-auto-sizing-table-cells
http://www.appcoda.com/self-sizing-cells/
Use a container view in a scrollView
Add constraints to superview (leading, trailing, top, bottom, height,width)
Make IBOutlet of constraints that you are going to update.
Add you all labels inside that view.
Update constrains/frame of your label so that it fits the text.
How much you increase the label height you should increase the container height too.
If the label count is not fixed use custom label class to add subview.
Perhaps you should need to understand how ScrollView works in Storyboard with autolayout.

iOS Auto layout problems with spacing

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. :-)

ios - UIScrollview w/ autolayout on Xcode 6

I have been struggling for days with this implementation, and even though I have tried to do every tutorial I found on the web, I still cannot make things work the way I want.
Basically, I am trying to put my login form in a scrollview, so that it takes the whole screen at first (and on all iPhones / iPads), and if the keyboard appears everything should move. The problem IS, my view doesn't take the whole screen... Either it is too large, or too high, even though in Interface Builder everything seams correct (from layout to constraints). Below and image of the layout I want to achieve (I am using an universal storyboard, with Size Classes and Autolayout enabled):
http://img4.hostingpics.net/pics/829115app.png
Can someone point me out on achieving this layout ?
Thanks in advance.
I would suggest pinning top, leading and trailing spaces of your scroll view to its superview. And set a bottom space constraint less or equal to the keyboard's height if you set it to 0, the scroll view won't be able to resize.
With your form layout set vertical center constraints and top space to superview constraints for your top label being more or equal than the distance you set in the IB, and then you can set relative space constraints between each of the components.
Hope I answered your question.
Edit: Just the provided project and got it working. I think the problem is caused by it being a containerView inside a scrollView. And both the container and the scrollViews content view adapt to the size of its subviews. Because of that, setting relative constraints won't help.
What I did was to set an explicit size (screen's size) to the containerView and setting setTranslatesAutoresizingMaskIntoConstraints(true) to it.
I modified your project and uploaded it here

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).

Top Space Constraint on UIView but not UIImag

I'm having a heck of a time with using some of these constraints. I'm simply trying to add a UIView in the IB so that I can inject other views into as needed. However, I can not get the positioning right. No matter how many Constraints I add or remove I cannot seem to remove the 'Top Space to: Superview'. I'm simply trying to pin the UIView to the bottom and have it float up for the different screen sizes. This 'Top Space' is forcing the UIView off the screen and I can't seem to remove it nor de-prioritize it.
UIView Constraints (doesn't work) - sorry can't post images yet
http://i1322.photobucket.com/albums/u574/dneely79/uiview-constraints_zpscae66ed9.png
On another note though I can remove the UIView and add a UIImageView and I can get this to work. So, for now I'm actually injecting my views into this element instead of a UIView. Its working, just feels dirty.
UIImageView Constraints (Works)
http://i1322.photobucket.com/albums/u574/dneely79/uiimageview-constraints_zps726da545.png
Anyone else have any thoughts on how to overcome this? Its been driving me mad and doesn't seem to make much sense to me.
I've already tried this help link (though trying to anchor bottom and not top)
Xcode 4 and Interface Builder: Editing Vertical Spacing Constraint (Anchor Top, not Bottom)
Thanks ahead of time for any help or advice on this matter.
This was a semi-goofy mistake on my part based on the defaults that IB gives you. I went back and better analyzed my own pictures.
After noticing a height set on the UIImageView and not UIView, I added a height and was able to successfully delete the Top Space.

Resources