aspect ratio for imageView in dynamic tableViewCell - ios

I've got a tableView with 4 dynamic prototype cells: three of them for different kind of labels and one for an imageView.
In viewDidLoad() I use this code to determine the height of each cell based on their content:
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
The problem is for the cell with the imageView.
The picture to be displayed occupies almost all the cell and it needs to be shown in landscape keeping it's aspect ratio.
I was able to do it, setting top, bottom, trailing, leading and height constraints: in this way the tableView could calculate the appropriate row.height.
Switching from iPhone 6 to 4, the image width is reduced but the height remains the same resulting in strange ratios.
I need to calculate the eight of the view based on the width of the moment and I tried it in two ways:
creating an NSLayoutConstraint outlet for the height. If I assign it a static value it works but when I try to calculate like:
constraintOutletForTheHeight.constant = CGFloat(myImageView.frame.size.width / 2)
it starts showing the first cells with the old value and the others appear with the computed height value.
I also attempted setting the aspectRatio constraint from Storyboard but in both cases Xcode tells me that it can't simultaneously satisfy constraints.
Thank you for your interest

After a lot of println() I found where I was wrong and a solution for that (not the most elegant but at least it works).
I discovered that the starting width of the imageView is the one assigned via storyboard (even after I set the imageView.image property) and after a bit of scrolling, it switches to presumed value: this is why I got constraint errors.
As a solution in tableView:cellForRowAtIndexPath:, I used the tableView width property like this:
cell.<image_view_height_contraint>.constant = CGFloat(tableView.frame.size.width / 2)
If you know a better solution you're welcome otherwise I hope it could help someone else.

Related

TableViewCell with multiple columns

I have an application in which I want to have a tableView with custom tableViewCell that has five columns.
I want them to be like 20%, 30%, 15%, 20% and 15% of the whole cell's width, because I want my application to launch on iPhone and iPad.
What is the best way to do this?
Now I have created a tableViewCell prototype with 5 labels. I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them.
Here is a screenshot.
You can create table view cell with collection view. By that, you can get different part in cell as well as you can change each column width programmatically based on your needs.
Thanks
A table view has only one column and allows vertical scrolling only. link
If you want to use a few columns, you can use UICollectionViewController
I also added constraints to them but the width constraint is just a constant. Also when I added width constraints to labels, some collisions appeared there and I don't know how to fix them
To fix it change width constraints to be a multiplier of superview width.

Setting constraints for dynamic cell

I'm new to using constraints in my iOS projects, and trouble setting the right constraints for my dynamic UITableViewCell. I've tried every combination I can think of but it either won't dynamically change height, or it gives me warnings about ambiguous layouts.
My first label1 is not supposed to change in height, but the other two are. My current constraints achieve the desired effect, but are giving me the warnings seen under.
The warnings go away if I constrain the height of the labels (obviously), but that doesn't solve my problem.
Any suggestions on how to solve this would be appreciated!
First of all you need to set
tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 44.0
in viewDidLoad of your UITableViewController
Then you need to set constraints from top to bottom of the cell
top space to superview
vertical spacing
bottom space to superview
height greater or equal
and of course set lines to 0 for every UILabel
Example project is available here https://github.com/MihaelIsaev/SwiftAutoResizableCells

UICollectionView min spacing not followd

I am explicitly setting the minimum cell spacing in the Storyboard to be 4, and the Storyboard's layout shows this:
But at runtime the vertical spacing is zero. (I have verified that the squares are still real squares.)
This has me losing hairs.
I ran into this same problem and was able to figure out what was going on.
If you are programmatically setting the size of the cells, make sure that the size of those cells is also updated in the collectionview of Storyboard.... Once these two places where the cells match, then the spacing can be created correctly.
Chances are the space is different because your cell width and height are greater in storyboard.

Multiple UILabels inside a self sizing UITableViewCell

In this iOS 8 app I'm creating, I have a tableview and I need them to be self resizing. I implemented it using Auto Layout and it works. Almost. Here's how it looks now.
There are 3 labels inside a cell. Main label which has the lorem ipsum text. Subtitle which has the string of numbers (Those are two separate labels. Might be confusing because they have the same color.) Then the third label with the small black text.
The first label resized itself correctly with no problem and the second label moves up and down accordingly. But the problem is with the third small label. As you can see, its not resizing itself to fit all the text.
Now there's a weird thing happening. I turn it landscape and here's it is.
Since there is space the label is displaying the entire text its supposed to. Fine. Then I turn it back to portrait.
Now the small label has resized itself to fit all its text but it overflows the cells boundaries. I tried making the cell bigger but it didn't work. Since this is self sizing cells, I don't think that's the correct way even.
I'm not getting any errors or even warning on my auto layout constraints either.
I have set these two lines of code in the viewDidLoad() method.
tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
Can anyone please tell me what I might be doing wrong here?
Since its difficult to answer just by looking at images and I don't have any more code to post beside the above snippet, I uploaded a runnable Xcode project demonstrating the issue here. (There are 2 custom cells. Basically its the same cell just the height is increased in the second one.)
I've been fiddling with auto layout constraints but I can't seem to get this working. Any help would be appreciated.
Thank you.
UPDATE:
With the help of this tutorial I found some helpful pointers. According to it, each subview should have constraints that pin all its sides and there should be constraints that goes from top to bottom which helps auto layout to calculate the height of the cell. In my original post, I had vertical spaces between each label so I think that's the reason auto layout couldn't calculate the proper height.
So I made some changes.
I reduced the vertical space between labels to 0 and set the Vertical space constraints between top and middle labels and middle and bottom labels.
I added leading, top, trailing constraints to the top label.
Leading and trailing to the middle label.
Leading, bottom, trailing to the bottom label.
Now here's another weird part. When I first run it, the bottom label cropping issue is still there.
But if I rotate the device to landscape and turn it back to portrait, all the all the cells are resized properly to fit both labels!
Still can't figure out why this doesn't happen at first though. Updated Xcode project is here.
The issue here is with the multi-line labels' preferredMaxLayoutWidth property. This is the property that tells the label when it should word wrap. It must be set correctly in order for each label's intrinsicContentSize to have the correct height, which is ultimately what Auto Layout will be using to determine the cell's height.
Xcode 6 Interface Builder introduced a new option to have this property set to Automatic. Unfortunately, there are some serious bugs (as of Xcode 6.2/iOS 8.2) where this is not set correctly/automatically when loading a cell from a nib or Storyboard.
In order to work around this bug, we need to have the preferredMaxLayoutWidth set to be exactly equal to the final width of the label once it is displayed in the table view. Effectively, we want to do the following before returning the cell from tableView:cellForRowAtIndexPath::
cell.nameLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.nameLabel.frame)
cell.idLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.idLabel.frame)
cell.actionsLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.actionsLabel.frame)
The reason that just adding this code alone doesn't work is because when these 3 lines of code execute in tableView:cellForRowAtIndexPath:, we are using the width of each label to set the preferredMaxLayoutWidth -- however, if you check the width of the labels at this point in time, the label width is totally different from what it will end up being once the cell is displayed and its subviews have been laid out.
How do we get the label widths to be accurate at this point, so that they reflect their final width? Here's the code that makes it all come together:
// Inside of tableView:cellForRowAtIndexPath:, after dequeueing the cell
cell.bounds = CGRect(x: 0, y: 0, width: CGRectGetWidth(tableView.bounds), height: 99999)
cell.contentView.bounds = cell.bounds
cell.layoutIfNeeded()
cell.nameLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.nameLabel.frame)
cell.idLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.idLabel.frame)
cell.actionsLabel.preferredMaxLayoutWidth = CGRectGetWidth(cell.actionsLabel.frame)
OK, so what are we doing here? Well, you'll notice there are 3 new lines of code added. First, we need to set this table view cell's width so that it matches the actual width of the table view (this assumes the table view has already been laid out and has its final width, which should be the case). We're effectively just making the cell width correct early, since the table view is going to do this eventually.
You'll also notice that we're using 99999 for the height. What's that about? That is a simple workaround for the problem discussed in detail here, where if your constraints require more vertical space than the current height of the cell's contentView, you get a constraint exception that doesn't actually indicate any real problem. The height of the cell or any of its subviews doesn't actually matter at this point, because we only care about getting the final widths for each label.
Next, we make sure that the contentView of the cell has the same size as we just assigned to the cell itself, by setting the contentView's bounds to equal the cell's bounds. This is necessary because all of the auto layout constraints you have created are relative to the contentView, so the contentView must be the correct size in order for them to get solved correctly. Just setting the cell's size manually does not automatically size the contentView to match.
Finally, we force a layout pass on the cell, which will have the auto layout engine solve your constraints and update the frames of all the subviews. Since the cell & contentView now have the same widths they will at runtime in the table view, the label widths will also be correct, which means that the preferredMaxLayoutWidth set to each label will be accurate and will cause the label to wrap at the right time, which of course means the labels' heights will be set correctly when the cell is used in the table view!
This is definitely an Apple bug in UIKit that we have to workaround for now (so please do file bug reports with Apple so they prioritize a fix!).
One final note: this workaround will run into trouble if your table view cell's contentView width doesn't extend the full width of the table view, for example when there is a section index showing on the right. In this case, you'll need to make sure that you manually take this into account when setting the width of the cell -- you may need to hardcode these values, something like:
let cellWidth = CGRectGetWidth(tableView.bounds) - kTableViewSectionIndexWidth
cell.bounds = CGRect(x: 0, y: 0, width: cellWidth, height: 99999)
I met the same issue as you and I found a simple solution to resolve it.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// dequeue cell...
// do autolayout staffs...or if the autolayout rule has been set in xib, do nothing
[cell layoutIfNeeded];
return cell;
}
And the self-sizing worked well. In my code, I laid two labels in vertical, both of them are dynamic height. The height of cell is correctly set to contain the two labels.
Assuming you don't have any errors with your constraints as others have suggested, this problem seems to stem from using a UILabel that allows multiple lines in conjunction with a UITableViewCellAccessory. When iOS lays out the cell and determines the height, it does not account for the offset change in width that occurs because of this accessory, and you get truncation where you wouldn't expect to.
Assuming you want the UILabel to extend the full width of the content view, I wrote up a method that fixes this for all font sizes
-(void)fixWidth:(UILabel *)label forCell:(UITableViewCell *)cell {
float offset = 0;
switch ([cell accessoryType]) {
case UITableViewCellAccessoryCheckmark:
offset = 39.0;
break;
case UITableViewCellAccessoryDetailButton:
offset = 47.0;
break;
case UITableViewCellAccessoryDetailDisclosureButton:
offset = 67.0;
break;
case UITableViewCellAccessoryDisclosureIndicator:
offset = 33.0;
break;
case UITableViewCellAccessoryNone:
offset = 0;
break;
}
[label setPreferredMaxLayoutWidth:CGRectGetWidth([[self tableView]frame]) - offset - 8];
}
Simply put this in your cellForRowAtIndexPath
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
#Setup the cell
...
// Fix layout with accessory view
[self fixWidth:[cell label] forCell:cell];
return cell;
}
Do this for any labels that are going to have multiple lines to adjust the width properly and then recalculate the appropriate heights. This works with dynamic font sizes as well.
Like smileyborg had mentioned, if you weren't using the full width of the contentView you could reference the constraints and subtract them from the width as well.
Edit: I previously was running 'layoutIfNeeded' on the cell but this was creating performance issues and didn't seem to be needed anyway. Removing it hasn't caused any problems for me.
You have two problems here.
1/ Right now, using Visual Format Language, your cell's vertical constraints can be translated like this:
Cell: "V:|-(10)-[nameLabel]-(67)-|"
Then, you set a second group of constraints:
Cell: "V:|-(10)-[nameLabel]-(8)-[pnrLabel]-(2)-[actionsLabel]"
Those two groups of constraints can't mix well and will reveal their ambiguity with your second problem.
2/ For some reasons, actionsLabel is limited to one line when you launch your app. Then, when you rotate your device to landscape mode, actionsLabel accepts to be displayed with two lines or more. Then, when you rotate your device back to portrait mode, actionsLabel keeps displaying two lines or more. But, because actionsLabel is not really part of your cell's height constraints, it overlap your cell's boundaries.
If you want to solve all those problems, I recommend first that you rebuild your xib file from scratch. This will cure your actionsLabel strange behavior (two lines or more only when you rotate your device).
Then, you will have to define your constraints like this:
Cell: "V:|-(10)-[nameLabel(>=21)]-(8)-[pnrLabel(>=21)]-(2)-[actionsLabel(>=21)]-(10)-|"
Of course, you can define other minimum height constraints for you labels than (>=21). In the same way, your bottom margin can be set to another value than -(10)-.
Addendum
In order to answer your question, I created a simple project with the previous constraints pattern in my .xib file. The following image may help you build your own constraints.
I tried the very easy and elegant looking solution of "fogisland" - and it did not work. Luckily I found out that one additional line makes it work in all directions. Just tell the system that you not only suggest a new layout (layoutIfNeeded), you explicitly ask for it (setNeedLayout)
cell.setNeedsLayout()
cell.layoutIfNeeded()
return cell

Please explain the following auto layout behavior to me

I'm currently trying to figure out what I am doing wrong with the following constraint-based UITableViewCell layout (iOS 8).
My cell is laid out as shown in this image:
There is an image view on the left, a label on the right, and both should be touching the cell margins everywhere. The image has a fixed size (64x64), the label's height will always be smaller than that. I want the image's height to cause the cell to expand the height to the correct value (image height + 2 * margin).
The problem is this: I have three constraints for the vertical size, V[Image(64)], Reset.bottom == UITableViewCellContentView.bottomMargin and Reset.top == UITableViewCellContentView.topMargin (all defined via the storyboard). When I display the cell, I get an unsatisfiable constraints error. The UIView-Encapsulated-Layout-Height constraint interferes with my constraints, and auto layout breaks my image view height constraint. Everything looks as it should, but I don't like errors at runtime.
If I give my height constraint the priority 999, everything looks fine, no errors.
So my understanding is, that my height constraint will be broken in both cases at runtime.
But when I delete the height constraint altogether, the image displays at its original height (the file has different resolution than what I display in the cell). Why is this, or what am I getting wrong here?
Edit/Update: I just noticed I was wrong about everything working. The initial display of my cell is fine, but when the cell changes (in my case I am moving another, simple single line label, cell below it) my cell changes its height to the default row height (44 pts), squashing the image in the process. This happens in both cases, when auto layout breaks my constraints or when I reduce the priority of height to 999. I'm really at a loss as to how to get these cells to do what I want.
You should try setting the estimatedRowHeight property in code, and also, depending on which version of iOS 6 you're using, set the rowHeight property to UITableViewAutomaticDimension,
self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;
The default rowHeight for nib or storyboard table views, is supposed to be changed to UITableViewAutomaticDimension at some point; I don't know if it has yet in 6.0.1, so that last line may or may not be necessary.
Are you supporting older version of iOS? If that is the case, be sure to uncheck the Relative to margin option for the constraints that are complaining.

Resources