How to remove the default padding of content view in UITableViewCell?
I put two label in the UITableViewCell's content view, The content view has a gray background color.
when I set top margin space between title and the content view to 0 (via constraint), the title is not align to the top!!! The same is leading and trailing space.
Here is my view struct:
I googled a lot, the answer in this post works by overriding the layoutSubviews of UITableViewCell and setting frame of title manually.
But can I do it in story board? or it there a attribute or something that controls the padding of the content view????
There is a checkbox labeled "Constrain to margins" in the popup where you specify constraints. I suspect you left it checked. Margins default to 8 points, which may explain what you are seeing.
Here is a better answer with screenshots:
https://stackoverflow.com/a/26755511/4122965
Related
I added a button to my view in viewcontroller and I gave it only positioning constraints (which are top space and leading space). In my button I added a set, an image and a title which is three four words. I have other views in the same horizontal row. I set their title and edge insets programmatically so both are aligned to center and title is just below the button.
How can I set its title dynamically so it will show full title? I have already used sizeToFit function that doesn't help.
Determine the size of your button title label using boundingRectWithSize:options:context:, then set the UIButton.frame accordingly.
Apple docs - boundingRectWithSize:options:context:
Edit:
Autolayout resize button based on text and have textfield fill available space
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 am making filling a UITableView with custom cells of dynamic heights. I am using the methodology as described here.
For the purposes of simplicity, say my cell contains 2 UILabels as shown below.
The purple UILabel will remain the same height for all cells in the UITableView. In this case, the purple UILabel is taller than the green UILabel. I would like the constraint between the bottom of the purple UILabel and the bottom of the contentView to be 8.
However, there is a second scenario that is possible as shown below.
In this case, the purple UILabel is still the same size as the first case (even though it looks smaller, but since the overall cell height is larger, relatively, it looks smaller), however the green UILabel is much larger. In this case, I would like the constraint between the bottom of the green UILabel and the bottom of the contentView to be 8.
Now my dilemma is that I can set either one or the other (I am using storyboard). But I cannot think of a way to make both possible at the same time. My guess is that you would need to adjust constraints programmatically during runtime depending on the size of the green UILabel.
Any thoughts?
I am using XCode 6.1.1 in Swift.
Thank you.
If the purple label is always the same height, then you should just set the height constraint of the element, rather than setting its bottom constraint to superview as 8.
For the green element, perhaps you could select both that element and the contentview and set them as having equal heights from the "add new constraint" pane at the bottom of the storyboard window. Then, select that constraint and edit its multiplier value in the attributes inspector to some value less than 1. That would make the element's height always be some percentage of the parent view's height. You could tweak that around until it gets you close to the desired 8px from the view below it.
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
I have normal UIViewController on my storyboard with a standard UIView which contains only one child - UITableView
When adding constraints between UIView and UITableView ( equal widths, equla heights, vertical space 0 top layout guide, horizontal space 0 for leading margin ) as a result my table view should fill it's parent.
Instead my table view appears to have left and top margins (16 and 64). The Size Inspector shows that the table views Alignment Rectangle is as follows x=16,y=64,width=600,height=600 although all my constraints have constants set to 0's. When I try to edit manually Alignment Rectangle I keep getting Misplaced Views warnig which try to restore previous values.
Any I idea what might be the cause of this strange behaviour?
Click the view you want to remove margins. Open Contraint Editor uncheck Prefer Margin relative:
I had a very similar problem. It was solved by deselecting Under Top Bars under Extend Edges in the properties of the topmost UIViewController in storyboard.