68067

UITableViewAutomaticDimension with programmatically added images

I have a problem with UITableViewAutomaticDimension:

In my tableview I have multiple imageviews created programmatically with calculated height:

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "managePlayset", for: indexPath) as! ManagePlaysetTableViewCell let playset = playsets[indexPath.row] cell.playsetName.text = playset.name if (playset.musicItems?.count)! > 0 { var i = 0 for musicItem in (playset.musicItems?.array)! { let imageView = UIImageView(image: (musicItem as! MusicItem).getFirstPhoto()) imageView.frame = CGRect(x: cell.contentView.frame.origin.x+CGFloat(i)*(imageWidth+10.0), y: cell.contentView.frame.origin.y+40, width: imageWidth, height: imageWidth) imageView.contentMode = .scaleAspectFit cell.contentView.addSubview(imageView) let constraint = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.bottomMargin, relatedBy: NSLayoutRelation.equal, toItem: cell.contentView, attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, constant: 0) cell.contentView.addConstraint(constraint) i = i + 1 } } cell.editButton.tag = indexPath.row return cell }

In viewDidLoad I set:

let width = view.frame.width imageWidth = width / CGFloat(maxItemsOnPage) - 10.0 tableView.estimatedRowHeight = imageWidth + 20.0 tableView.rowHeight = UITableViewAutomaticDimension

But height is not adjusted properly (images are cut off) Any advice is highly appreciated!

Answer1:

I think it's because UIImageView created programmatically. Then it don't use autolayout system. To change it, you have to add imageView.translatesAutoresizingMaskIntoConstraints = false in your loop.

Answer2:

Have you tried constraining both top and bottom edges of the imageView? i.e.

let constraintTop = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.top, relatedBy: NSLayoutRelation.equal, toItem: cell.contentView, attribute: NSLayoutAttribute.topMargin, multiplier: 1, constant: 0) let constraintBottom = NSLayoutConstraint(item: imageView, attribute: NSLayoutAttribute.bottom, relatedBy: NSLayoutRelation.equal, toItem: cell.contentView, attribute: NSLayoutAttribute.bottomMargin, multiplier: 1, constant: 0) cell.contentView.addConstraints([constraintTop, constraintBottom])

Recommend

  • Unable to display certain images on LaunchScreen
  • Unload image of UIImageView thats offscreen
  • How to get a android ListView item selector to use state_pressed
  • UITableViewCell subclass wrong image in cell or old image bug
  • Picasso Taking time to load images
  • Small video playback
  • Python Tkinter after event OOPS implementation
  • Dynamic ranges again - once more, with text strings
  • insert radio value multiple data codeigniter in database to one row?
  • What's the syntax to inherit documentation from another indexer?
  • Drawing a Path in XAML (with binding) vs. C#. One works, one doesn't
  • ggplot2: make the points on the line a darker color than the line color
  • Add dynamic data to line chart from mysql database with highcharts
  • Row to Column conversion in Talend
  • Using Sax parsing to edit and write XML in VB6
  • PLSQL: Get number of records updated vs inserted when a merge statement is used
  • Low TTL with Leveled Compaction, should I reduce gc_grace_seconds to improve read performance withou
  • Query to find the duplicates between the name and number in table
  • gspread or such: help me get cell coordinates (not value)
  • MongoDb aggregation
  • Django: Count of Group Elements
  • recyclerView does not call the onBindViewHolder when scroll in the view
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • How to set my toolbar fixed while scrolling android
  • Display Images one by one with next and previous functionality
  • Rearranging Cells in UITableView Bug & Saving Changes
  • align graphs with different xlab
  • Unanticipated behavior
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to delete a row from a dynamic generate table using jquery?
  • Benchmarking RAM performance - UWP and C#
  • using HTMLImports.whenReady not working in chrome
  • Angular 2 constructor injection vs direct access
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Can't mass-assign protected attributes when import data from csv file
  • Unable to use reactive element in my shiny app
  • Android Heatmap on canvas or ImageView