I have a static UITable view with 4 cells: the first and last cells are custom, while the second and third cells use a built-in style ("Basic" and "Right Detail", respectively). I have the following constraints on the label in the first and last cells:
label.leading = superview.leading + 20
label.Center Y = superview.Center Y
I'm having trouble matching the constraints for my custom UITableViewCell labels to those of the default UITableViewCells (the ones using the built-in styles).
For certain devices (e.g iPhone XR) the X position of the labels match for all four cells (20):
But for other devices (e.g. iPhone Xs) the X position of the labels do not match (16 vs 20):
Note the X position of the first and last cell is greater (20) than that of the second and third cell (16).
At first I thought that the constraints for the default cells were being varied based on the size classes of the device, but both the iPhone XR and Xs have compact width (wC) and regular height (hR).
Any idea how I can match my custom cell's label constraints to those of the default UITableView cell's label?
textLabel and detailTextLabel are present even in a subclassed UITableViewCell. While using textLabel instead of a custom label would solve the left margin issue, there is still a problem with UISwitch on the right side.
However, you could align your layout with a leading constraint of textLabel and a trailing one of detailTextLabel.
if let leadingAnchor = cell?.textLabel?.leadingAnchor {
myLabel.leadingAnchor.constraint(equalTo: leadingAnchor).isActive = true
}
if let trailingAnchor = cell?.detailTextLabel?.trailingAnchor {
mySwitch.trailingAnchor.constraint(equalTo: trailingAnchor).isActive = true
}
Related
hello guys now I have UITableViewCell should have dynamic height , this cell have 3 labels , I have added them in to stack and I added all requirements
to let tableViewCell be dynamic height
1- The code used for automatic cell height
notifications.rowHeight = UITableView.automaticDimension
notifications.estimatedRowHeight = 150
2- I have set constraints leading and trailing and bottom and top space to the superView.
3- The number of lines for each label is equal zero
this is cell before run
enter link description here
this cell after run https://ibb.co/5YCMpfK
The constraints of your tableViewCell must be like:
Properties of stackView:
Axis: Vertical
Alignment: Fill
Distribution: Fill Proportionally
Spacing: 5 (as per your requirement)
Number of lines of all 3 labels = 0
Check if your stack view doesn't have height constraint.
Check if your labels do not have any height constraint.
Give your stackview top and bottom constraints a specific value and make them greater than equal to topContraint >= 5 bottomconstraint >=5.
Change the property of stack view to Fill proportionally and give spacing according to your needs.
I have a custom table cell. It has two labels (title and description), one below the other.
What I have right now is, title label top = topMargin. And description label top = title label bottom + 10.
But in some cases, there will be no description. In such cases, I want the title label to be centered vertically inside the cell. Is this possible? What constraints do I have to set?
A UIStackView makes it easy to do what you want.
Create a prototype cell
add two labels
embed them in a stack view
set the stack view's properties to:
Axis: Vertical
Alignment: Fill
Distribution: Fill Equally
Spacing: 10
constrain the stack view Top/Leading/Trailing/Bottom to 0 to the cell's content view's default margins (or set your own "padding")
set your fixed row height - with default labels + 10-point spacing + top and bottom margins, you'll probably want at least 67
connect the labels to IBOutlets
When you set the text of the labels in cellForRowAt indexPath:, set the description label's .isHidden property to true if it has a description, or to false if it doesn't.
The result (with background colors for clarity):
The result without background colors:
I have 2 labels: the description label (w/ red background) and the results label (gray text)
How do i set constraints for this example in order to have the results label with the size of its content and the description label until the results leadingAnchor? (like i have in the second row)
Objective C
[self.customTextLabel.trailingAnchor constraintLessThanOrEqualToAnchor:self.counterLabel.leadingAnchor].active = YES;
[self.counterLabel.widthAnchor constraintGreaterThanOrEqualToConstant:0].active = YES;
swift
titleLabel.trailingAnchor.constraint(lessThanOrEqualTo: counterLabel.leadingAnchor).isActive = true
counterLabel.widthAnchor.constraint(greaterThanOrEqualToConstant: 0).isActive = true
I have a solution that i think it's ugly.
self.counterLabelWidthConstraint = [self.counterLabel.widthAnchor constraintEqualToConstant:0];
self.counterLabelWidthConstraint.active = YES;
And then after i set the text:
self.counterLabelWidthConstraint.constant = [self.counterLabel sizeThatFits:CGSizeMake(CGFLOAT_MAX, self.counterLabel.height)].width;
The way to do this with auto layout is by using the contentCompressionResistancePriority of the 2 labels. Set the pririty to NSLayoutPriorityRequired for the second label and something lower like NSLayoutPriorityDefaultLow for the first label. Then, as long as the 2 labels have proper constraints anchoring them to their superview and each other, the first label should compress while the second label should not.
You just need to increase the horizontal compression resistance of the right/gray label to be higher than that of the left/red label. This tells the visual layout that, in the event that there is not enough space for both labels, the one on the left will be compressed before shrinking the label on the right. 750 is the default for all views, so just increase the right/gray label's horizontal compression resistance to 751 and you should be good to go.
Swift 5 programatically:
<#label#>.setContentCompressionResistancePriority(.required, for: .horizontal)
Labels with this property will not compress horizontally.
You can set constraints for in storyboard itself. Select Label 1 (red back ground) and label's superview set widths are equal constraint. Select Label 1 and double tap on its width constraint, from the resultant window, you could see Lable 1 width equal to superview with value constant as '1'. change '1' to 0.7 or whichever the percentage you want.
How to evenly space two subviews around the centre of UIStackView in the vertical axis?
Currently both labels are next to the top and the bottom of the StackView. The stack view is the greyed area on the simulator screen shot.
How to make them evenly spaced?
|
|
[UILabel]
|
|
[UILabel]
|
|
I've tried different configurations, but I couldn't get the desired result.
Here is my current code:
let object = UIStackView()
object.translatesAutoresizingMaskIntoConstraints = false
object.alignment = UIStackViewAlignment.Center
object.axis = .Vertical
object.distribution = UIStackViewDistribution.EqualSpacing
Considering you have similar properties set to UIStackView (as mentioned in the question)
alignment -> Center
axis -> Vertical
distribution -> Equal Spacing
The solution can be achieved by introducing two empty views with height constraint set to 0 to both ends of UIStackView.
Explanation
On Adding two empty views of height 0, we are telling UIStackView that we have 4 views instead of 2.
Now UIStackView will add equal spacing between all the views and hence the output.
You can cross verify it by looking at the height of the UIStackView and the position of the labels.
Hope this helps !!!
In stackView attribute inspecter change spacing value from 0 to your preferred space value..
I'm having difficulty in adding the constraints to these 2 labels. I want them to be multiline and fit accordingly in the tableview cell. So what constraints should I put?
1.for 'first label' add left and top constraints to Content view of cell with value = 0 ;
2.and also for 'Second label' add right and top constraints to Content view of cell with value = 0 ;
3.create a horizontal spacing between labels.
4.and use UITableViewAutomaticDimension at heightForRowAtIndexPath