UITableViewAutomaticDimension not working on iOS 8
add following code in "cellForRowAtIndexPath" method before return cell.
cell.setNeedsUpdateConstraints()
cell.updateConstraintsIfNeeded()
UITableViewAutomaticDimension not working for footer in iOS8
After test, here is my results
For iOS8, UITableViewAutomaticDimension
only work for section header and tableview cell and not work for section footer
BUT strangely, if my tableview use both section header and footer, the UITableViewAutomaticDimension
will work for footer.
Then here is the solution
- (void)viewDidLoad {
// I set UITableViewAutomaticDimension for both footer and header
self.tableview.estimatedSectionHeaderHeight = 0; // it should be 0 because header is empty
self.tableview.sectionHeaderHeight = UITableViewAutomaticDimension;
self.tableview.estimatedSectionFooterHeight = 140;
self.tableview.sectionFooterHeight = UITableViewAutomaticDimension;
}
// I return empty view for header
- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
return [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
}
For iOS9 and above, UITableViewAutomaticDimension
work correctly
UITableViewAutomaticDimension not working with custom table cell
Automatic height works on calculated cell's contentView
height. You therefore have to add constraints that will be used to calculate its real height.
Try this:
class UINoteTabelViewCell: UITableViewCell {
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.selectionStyle = .none
}
func fill(_ note: Note) {
let view = UINote(withNote: note, atPoint: .zero)
self.contentView.addSubview(view)
// this will make the difference to calculate it based on view's size:
view.translatesAutoresizingMaskIntoConstraints = false
view.leftAnchor.constraint(equalTo: contentView.leftAnchor).isActive = true
view.rightAnchor.constraint(equalTo: contentView.rightAnchor).isActive = true
view.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
view.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
}
}
Also, estimatedRowHeight
should be set to a specific value that approximately estimates the size of all cells. But that would not be a problem.
why UITableViewAutomaticDimension not working?
In order to make UITableViewAutomaticDimension work you have to set all left, right, bottom, and top constraints relative to cell container view. In your case you will need to add the missing bottom space to superview constraint for label under the title
UITableViewAutomaticDimension not working until scroll
I ran into the same issue and found out that the accessory cell.accessoryType
messes with this automatic resizing when it is not None
, so it seems like a bug in Xcode at the moment.
But as @Blankarsch mentioned, calling reloadSections(..)
helps to fix this issue if you need to have an accessory.
Related Topics
Behaviour of Protocols with Self
Migration from Swift 3 to Swift 4 - Cannot Convert String to Expected String.Element
How to Cast [Int8] to [Uint8] in Swift
Array Extension Called from Other Module
Uibezierpath Appending Overlapping Isn't Filled
Implementing Swift Protocol Methods in a Base Class
Swiftui Sheet Gets Dismissed the First Time It Is Presented
Alamofire Not Handling Authentication Challenge
Call Function When If Value in Nsuserdefaults.Standarduserdefaults() Changes
How to Access Content View's Elements Later in Swiftui
How to Use Willset and Didset with Getter and Setter
Generic Method Override Not Working in Swift
Drag and Drop Image from Uicollectionview to Uiview
Is Gamescene.Sks Not Recommended for Game Building
Why Are Uiscreen.Bounds Incorrect in iOS11
Swiftui Classes That Conforms Observableobject Should Be Singleton
Using Nil-Coalescing Operator with Try? for Function That Throws and Returns Optional