dynamically load UICollectionView in xcode - objective-c

We have a label followed by a UIViewCollection. The height of the label is dynamically set based on its content. Based on the height of the label, we want to start the UIViewCollection as soon as the label ends. We are tracking the label x, y, width and height and adding some padding to y in the code to calculate the start of UIViewCollection. However, UIViewCollection does not move at all. Is it because of the specified storyboard? We tried it in ViewDidLoad and ViewWillAppear.
Also, if we replace the UIViewCollection with a label such that there are 2 labels one below another, we are able to dynamically load the start of the 2nd label if the code is placed in ViewWillAppear.
Please can someone help with moving the UIViewCollection up or down based on the previous label height?
Thanks in advance.

It may be because You use autolayout. You can switch off autolayout or set translatesAutoresizingMaskIntoConstraints YES for UIViewCollection, and than set current location

If you're using auto layout this is easy to do, and you don't need to do anything in code to keep track of the label's height. The label should have a height constraint to the top of the view and a spacing constraint to the top of the collection view (the length of that constraint will give you whatever padding you want between them). The collection view then needs a constraint to the bottom of the view. This setup will cause the collection view to get shorter when the label gets taller. If instead, you want the height of the collection view to stay the same, but move down (assuming that it's short enough that there's room to move down), give the collection view a fixed height constraint, and remove its constraint to the bottom of the view.

Related

How do I make constraints to UITableView with multiple cells with static and dynamic height?

I searched a lot about this, couldn't find an answer and I am stuck with this problem for some days already.
I have a UITableView with 3 sections, each one with custom cells (xib). The first section must grow accordingly with one of its labels.
The second one is static, it shows up just an static image and basic height.
The third one is like a comment cell, with UIImageView, labels etc. One of this labels must grow till a limit height.
I managed to show the 3 custom cells, but the first and the second one (which are not static height), show only partially. If I set manually the Height of the row I can see it entirely, but all section cells grow and it is not what I need.
The constraints to auto layout, I keep getting some conflicts but could not find any solution, tried to debug the view stack, no success.
Custom Cell
Constraints of custom cell
Can anyone help me or point me to a direction?
Thanks in advance!
I had the same problem. I solved it like this. Set tableView estimatedRowHeight to minimum row height. Set tableView rowHeight to UITableViewAutomaticDimension.
1) For dynamic cell height - Give the label leading, trailing, top, bottom and height greater than equals to minimum height.
2) For fixed cell height - Give the label leading, trailing, top, bottom and height equals to fixed height whatever you wanted.
By this your dynamic height cells will grow and fixed ones will be fixed. If you did not get what i said then post a sample project with just this custom implementation on git or somewhere else. I will correct it.
Thank you.

WatchKit Interface : Is it possible to add multiple labels in a tableView group without any of the labels being pushed to the right?

This is somewhat hard to explain, but basically I have one label in a tableView group that needs to take up the width of the group and placed at the top. I want to add a second label at the bottom of the group and take up the width of the group as well. However, I cannot do this. When I add my second label it gets pushed to the right automatically. Seems, the second label can't be on the same horizontal plane as the first label. Is there a way to resolve this. I can place my second label at the bottom, but it just gets pushed off to the right and off screen.
Add both labels to the group and then, set in that group vertical Layout (Attached screenshot). Also both labels should have width: relative to container
If you look at the group's settings in Interface Builder, you'll see an option to change the layout type to Vertical, rather than the default Horizontal. That should help you achieve the layout you're looking for, if I understand correctly.

IOS 7 Approach for create container with two elements?

So, i am new to xcode and iOS7 and i'm trying to create simple container with two elements inside.
I prefer to make it 100% programmatically. (no IB)
I want to create container with two elements Image and Label.
I want to achieve variable width depends on the text element inside.
Here is example:
Based on user action i want to change text on the fly. Let's assume longer text, and main container have to change width too.
And now the question is: What is best approach to to that?
UIView with subviews or something else i'm just expecting direction.
Code examples with be gratefully appreciated.
Thank you in advance.
What you have described is exactly what a UIButton does automatically: it is a container containing an image and a title (text) and it resizes itself automatically when the text changes.
Let us assume, however, that you want to do this yourself. That is, let's say you want a UIView ("container") containing two other UIViews (subviews). Then we need to discuss this requirement:
Based on user action i want to change text on the fly. Let's assume longer text, and main container have to change width too.
This is not going to happen automatically. You can use constraints (auto layout) to describe the size / position of the subviews in relation to the superview, but it works the other way: the superview changes, and the subviews obey. So you will have to change the superview size manually after you change the text.
You can still use auto layout to help you. Let's say the text is in a UILabel. Well, a UILabel wants to change width automatically when the text changes. So far so good. But you must still change the container view width yourself. You can call systemLayoutSizeFittingSize: to learn what the size of the container should be, using constraints, working from the inside out; but then you will have to change its size yourself to that size.
(You can easily create the view, the subviews, and the auto layout constraints in code.)
If you don't want to use autolayout, then you will just calculate the sizes and positions of everything when the text changes and adjust it all yourself (in code). You can learn the size the label needs to be, to fit its text, by calling sizeThatFits: (or sizeToFit which will actually resize it correctly).

Is there a way to shrink a constraint with autolayout without code?

Without using code, I'm basically trying to achieve the "Desired outcome" in this picture:
I want the constraint on the "Hello!" label to shrink its length when the screen height is shorter.
As you can see in "Actual result", the Button in the bottom is off the screen. I want the image view to have fixed width and height.
I know I can create IBOutlets for constraints and doing it by code like this kind of posts suggest (autolayout - make height of view relative to half superview height), but I'm trying to avoid using code as much as possible.
Side question: If there's no way to do this in IB, what are the best ways to do this in code?
Thanks for the help!
Yes. The easiest way to have this kind of "split height" constraint is to put a "header" view between your image view and the top of the screen, and embed your "Hello" label inside this new header view. Then add a constraint to keep the "Hello" label vertically centered inside the header view.
To keep the header view the right size, add constraints to keep the top of the header view pinned to the top of the screen, and the bottom of the header view to the top of the image view.
Then you'll just add constraints to keep the button and image view pinned to the bottom of the screen. (Or, see comment from Sulthan, below.)
In Xcode 5.1 (in beta), there is UI to make more general constraints, including the ones you want, but you have to do it like this or in code in earlier versions.

How to create labels with dynamic size in iOS 6 with autolayout

I'm having trouble with autolayout and labels that get their text set in the source code based on some external conditions. These layouts (portrait and landscape) look like this on the simulator:
http://imgur.com/l6Iirun
http://imgur.com/n7RwwSD
The second one is obviously not what I want it to be, the label with the URN should fill the whole screen.
I have a width constraint for the dynamic label added by Xcode which I can't get rid of even though the "Name"-label has a fixed width and all horizontal spaces are fixed as well which should logically rid me of the need to have a width constraint for the label containing the dynamic text.
Any help on how to achieve what I want? Thanks in advance!
It looks to me like your labels are not binding their trailing edge to the superview. In the lower right there is the little 'I beam' icon, click on your label and then that icon, the top of the menu should be 'Pin' and pick trailing edge to superview. The label should expand to fit the width of the screen, minus some padding. Once that is in place you should be able to remove the width constraint - although you may need to find it in the size inspector and select promote to user constraint before you can remove it.
If you don't see the pin menu, try going up to the top and picking Editor -> Pin and you should see it there.
If it is already pinned, try increasing the content compression resistance priority and decreasing the priority of the width - although if pinning it does not fix it then there is another constraint at play.