I created an UICollectionViewCell (.xib) and I just wanted to add a label and some simple constraints there, as such:
However, doing this messes up everything. No matter the size I chose on UICollectionViewDelegateFlowLayout, the cell will always use the size of the label! What am I doing wrong here? Am I supposed to not use constraints on CollectionViewItems and instead translatesMaskIntoConstraints = true?
Thanks!
edit: Here's what I expected:
And here's what I actually have (as soon as I add the constraints):
edit2: Fixed! By changing the label's content hugging priority to 10 (for example, or lower). But...why exactly if there are no other constraints in the view? If somebody can answer it, I would be very grateful, cause I could just answer this question and give this solution but I can't explain why exactly it fixed it, so it's not a proper answer anyway.
You have 2 ways: you can delegate to a cell to calculate own height or you can set fixed size for height.
If you want the first case, please, read other related answers
If you want fixed size, you should make a fixed height on the label. Label is calculating own size based on contentSize, so your constraints are failed in the matter of fixed size of your cell
Related
I have a custom UITableViewCell, it looks like this:
The warning I'm getting is:
Inequality Constraint Ambiguity: trailing >= date.trailing + 16
I highlighted the constraint in the picture above.
What I don't understand is, before 'someView' (the last view in the bottom) was added, there was no warning; When it was added without height = 20 constraint, there was no warning. However, as soon as the height constraint was added, I start to get the warning message.
I have to add the height constraint because without the it, the height of 'someView' is not 20 anymore.
If it helps, I am using
tableView.rowHeight = UITableView.automaticDimension
to decide the height of a row
I used the same UI of yours in UITableViewCell:
The fields with width , height and aspect ratio has constraints as :
and all the remaining `constraints are as follows:
The only difference is I guess the height constraint to Name Label. If I am adding that then I am getting constraint conflict issues. Removing that is not showing any issues and UI is also perfectly fine.
Hope this helps to figure out and fix your issue.
You’ve added a lot of unneeded heights for a dynamic cell. I honestly hate seeing labels with height constraints, as contentSize will set that height for you. When I used to run into layout issues, my Senior IOS Developer would tell me to delete it all and start again, lol. I would suggest this but it would be a terrible answer. It’s also bad practice to have placeholder text in your labels/views. For some reason interface builder hates it. Just some tips.
You can download the example here: http://s000.tinyupload.com/index.php?file_id=02066350701605990856
I know there was a lot of questions about it. But, I have problem with this anyway. I have UIImageView that needs to have specific size and UILabel that needs to have dynamic height. I want to show label on the right side of image. I try not to use storyboard, so here is how I gave constraints using EasyPeasy library:
myImage.easy.layout([Top(8), Left(20), Width(95), Height(95), Bottom()])
myLabel.easy.layout(Top().to(myPicture, .top), Left().to(myPicture, .right), Right(), Bottom())
Below is the result:
As you see, when the text is big, it doesn't fit in cell. I set numberOfLines as zero for label. As I've understood, my image defines the size of the cell and the label can only cover the area that it actually can have. So, what can be the problem? How to solve it? How to make label's height resizable, despite image height? Is order of constraints important?
I don't know exactly how EasyPeasy works but I think you should use something like this:
myImage.easy.layout([Top(8), Left(20), Width(95), Height(95), Bottom(>=0)])
myLabel.easy.layout([Top(20), Left().to(myPicture, .right), Right(), Bottom(20), Height(>=90)])
I just put constraints into my ViewController, and I set the UITextView so that it keeps its height and width, yet at runtime it shrinks to only cover the text inside of it. How can I avoid this?
Thank you!
In my opinion, at the run time, the size of your TextView has changed because of the content, I mean the text set to it. To prevent this kind of issue, I would suggest some options which will be regarding your UI
At first, if you intend to fix size of the TextView, so from the constraints or Interface Builder, please create width and height constraints then fix the value. For example
If you wish your text view will be dynamically in size based on contents, you can set width and height values are greater than or equal a minimum value as it will not break your UI
I hope this would be helpful.
I assume you're using Interface Builder. Check the value for Content Hugging Priority that IB set for the text view. If it's too high, it may be overriding your constraints.
This is the craziest problem I've ever faced in iOS development. Basically I have a xib file:
This xib file is loaded as a row in a UITableView. In this xib file I have an UILabel ("Texto do comentário"), that is a multiline uilabel and will expand according to its content. To do so, I've added the following constraints and content hugging:
And all this actually works fine when I run and replace the content with a big text. This is how it looks like:
OK, now it begins the weird things, if I scroll up to the other cell (that uses the same xib file), the text view expanded its width and height in a way that doesn't respect my constraints:
Now if I scroll back to my cell that was working fine before, it has the same problem, the uilabel's width and height are not respecting my constraints anymore:
I've tried to inspect what actually happens to the constraints, if they are being ignored by some reason, and here is what I've found out. Both the constraints of my uilabel that has the right size and the ones in the uilabel that has the wrong size are the same.
I'm really starting to thing that this an iOS bug. Thank you in advance for your help
After trying, at least everything. The fix I found is to use UITextView instead of UILabel. This fix made me be sure that this is a UILabel bug.
I would say the problem you have is that you changes the Content Hugging Priority and Content Compression Resistance Priority values, which will make the UILabel not resize as you would expect.
You could try resetting the values to the default ones and if your other constraints are correct, the height of the label and of the cell should be calculated correctly.
Probably the UITextView worked because you did not change the default values for these properties.
Hope this helps.
This looks like another bug I previously encountered with self-sizing cells in a table view. Originally, I worked around it by explicitly setting the preferredMaxLayoutWidth of my cell's labels but I then discovered that sending an -updateConstraintsIfNeeded message to my cell in -tableView:cellForRowAtIndexPath: also worked around the issue without needing to set the preferredMaxLayoutWidth.
Hope this helps.
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).