I have made a prototype cell for UICollectionView and set some dimension for that. In the cell I have added Thumbnail(and other objects) and set constraints so that thumnail fills the cell completely. However there is still some space left between. I have set background color in cell to demonstrate that. Attached screenshot for the same. Same thing happens in tableview as well.
One hack could be to set negative value in constraint. But I want to why extra space is coming, even constraint are set to match to cells top, left, bottom, right.
The extra spacing is due to margins, while selecting any constaint just uncheck relative to margin in selected constraints
open second item drop down
there will be relative to margin and
uncheck it
Your content of the cell has an x of 8 and y of 8 which will result in having a padding as when you attach a constraint to superviews, the constraints will add as margins. Just change the x and y to 0 and update the constraints!
[EDIT]:
To elaborate the point:
1.clear the constraints of the view inside the content view of the cell.
2. make the view's x and y as 0.
3. add constraints as previously, you did, UNCHECK "constraints to margin".
Related
can we set a constraint for setting UICollectionView height to relative of the Device Screen from storyboard ?? i can set it to the tableViewCell's height but how to set for the Screen Height ?? all i wanna do is make a relative constraint between Screen Height and the View(which is inside a TableViewCell) so that i can give it the hight of half of device screen
as you can see in the size inspector i can set it the constraint for my view to its superView which is TableViewCell
Your constraint is fine, you have to set the constraint as "Equal Height" between the two views, and the multiplier to 0.5 for the value to be halved. If the view 1 and view 2 aren't in the correct order (superview is half of collectionview), you must invert the "First" and "Second" views of the constraint.
From your screenshot I can see that all that looks fine, but you have a constraint conflict (red arrow). Read it and see what's wrong.
Usually that means you have a constraint that says "do X" and another that says "do Y" and are conflicting. If you know it's from your last constraint, remove it and think "why", you'll figure it out. If you don't know where it came from, chose between the given choices in the red arrow submenu. In your case, it could be the following :
Your collectionview is set to have both 50% of height, and full height, because you might have set "Top-Bottom-Left-Right" AND "height". here, you could remove bottom or top.
Just imagine yourself as Xcode, if I tell you where to put your collectionview on Left, Right, Top and Height, you don't need to know where the bottom arrives, because the height is enough. IF you do need a constraint for the bottom, it has to be set to something else than Equal, or have a lower priority.
Sometimes it means your view doesn't have all required constraints, for example "No Y constraint defined for collectionview". Just make sure you have set everything right after you've set the height.
Again, only the information in the red arrow menu (conflicting constraints) can help us determine the issue easily. I'm not gonna lie I don't really wanna read all your constraints from the left menu manually.
But with this information you should have more than enough to resolve this yourself.
I'm building a table view prototype cells defined in InterfaceBuilder, and there appears to be an 8 pixel margin below the content view in all the UITableViewCells being defined.
I'm able to align a view at the absolute bottom of a cell by setting the autolayout bottom parameter to -8, but this seems like kind of a hack and potentially brittle to future iOS updates, and it seems like there should be a way to get rid of the 8 pixel margin in IB or programmatically.
Check that you have not used a constraint which is pinned to the margin rather than the view.
If so, change the constraint to be pinned with no margin.
When you create the constraints for the outermost view there is a check box saying constrain to margin. Deselect that. On existing constraints you can also remove the margin use on the individual constraint menus for the first and second views.
I'm trying to add constraint in Storyboard to an UIImageView to it's superview, equal widths or heights but it's disabled.
I tried selecting both the view and the superview, then the add constraints button at the bottom right (mentioned constraints appear disabled), I tried dragging from view to the superview (mentioned constraints aren't listed).
I tried removing all the constrants related to my view. Didn't make a difference.
I'm using a xib, of a custom view. The parent view is also the root view. XCode 6.2 beta. Using size class Any/Any.
Any idea? Thanks.
For making UIView height/Width equals to super View.
Here's what you need to do.
You can set leading,top,tailing and bottom constraint by right clicking to View and select superview.
After setting Top and bottom constraints as i've set in previous image. You need to update constraints to '0' as it might have top and leading space not equal to '0'. You can select those constraints and changes constant to '0'.
You can also set constraints from pin to View as i've set for leading and tailing you can also set top and bottom option in similar way.
EDIT:
If you want to set width and height equally then you need to select both the view and set it's equal height and equal width. As I've set them in below image.
Result:
Below two images shows how you can changes width/ height using multiplier as you need to set constant to 0.9. as from storyboard or xib you cannot set constant in float. So you need to set multiplier.
First Image I've set multiplier in ratio.
Second Image I've set multiplier to 0.9
If you still face any queries please let me know. Hope this will help you solve your problem.
Don't give the inner view (UIImageview) any width or height
Add top margin (with superview) with 0
Add bottom margin (with superview) with 0
Add left margin (with superview) with -16
Add right margin (with superview) with -16
Well, I ended adding a parent view in between. This parent is constained to the top view - left, right, top, bottom: 0, and then I can set my inner view to equals width and hight to the parent (the reason I want to do that is to make proportional size - set constant to e.g. 0.9).
No idea why it's not possible to do this directly with the root view.
I'm having a strange issue with my tableview, debugging the view hierarchy i'm getting this
I've tried a lot of constraints with no result, how should I setup my tableview to avoid that kind of misplacement ?
Edit
I've a custom cell with an image view, actually set just this 5 constraints
Top, bottom, leading and trailing values are 0
You just need to set Your UITableView constraint to Leading,Trailing, Top and Bottom. and also for your UIImageView set same constraint. so your imageview will show fit to cell as per table width. no need to set Align Center X constraint.
I am playing with autoLayout constraints in iOS
My use case is this way,
I will better explain with images
I am using a custom cell for UITableViewCell, through autolayout I have set constraints for text and divider line below and they obey autolayout constraints. Now you can see the yellow vertical strip on the left.
I have not set any constraints for it, so when the cell height is normal, it covers the whole cell, but when cell height increases, it doesnt increase and fill the whole cell. I am not able to make its height flexible w.r.t to cell.
I want to expand the yellow strip to cover the whole cell.
So how I can achieve this.
You can make height of yellow strip flexible by setting 2 constraints.
Set both top space and bottom space to container (cell's content view) to 0.
Ensure there is no constant height constraint for it.
You need to set constraints to the yellow view. By default it would apply its own constraints at runtime.
To the yellow view add TOP , BOTTOM , LEADING , and TRAILING constraint. DO NOT ADD A HEIGHT constraint.
Make sure the TOP and BOTTOM constraints of the yellow view are pinned to the cell's content view.
Let me know if it works.
Cheers