Table view automatic dimension forcing duplicate constraints - swift

Getting automatic dimension working for UITableView.rowHeight requires a duplicate constraint in my UITableViewCell class.
I'm creating a UITableView programmatically (SwiftUI, no storyboard) and have cells of different heights. I’ve set the table’s rowHeight to UITableView.automaticDimension but can’t find the correct combination of constraints to get the table to actually calculate the correct height for the cells without adding a duplicate constraint.
I would expect to add a width, height, top, and leading constraint to get things working correctly. However, the table does not size the rows properly unless I also add a bottom constraint. Naturally this produces the warning:
2019-10-23 18:06:53.515025-0700 Test[15858:7764405] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want.
Try this:
(1) look at each constraint and try to figure out which you don't expect;
(2) find the code that added the unwanted constraint or constraints and fix it.
(
"<NSLayoutConstraint:0x600001651c20 V:|-(0)-[UIView:0x7f8fc5a08890] (active, names: '|':Aries.TestTableViewCell:0x7f8fc5a084e0'TestTableViewCell' )>",
"<NSLayoutConstraint:0x600001651e50 UIView:0x7f8fc5a08890.bottom == Aries.TestTableViewCell:0x7f8fc5a084e0'TestTableViewCell'.bottom (active)>",
"<NSLayoutConstraint:0x600001651ea0 UIView:0x7f8fc5a08890.height == 40 (active)>",
"<NSLayoutConstraint:0x600001652120 'UIView-Encapsulated-Layout-Height' Aries.TestTableViewCell:0x7f8fc5a084e0'TestTableViewCell'.height == 40.3333 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x600001651ea0 UIView:0x7f8fc5a08890.height == 40 (active)>
If I remove the height constraint or the bottom anchor constraint the duplicate constraint is gone and the warning goes away. However, then the table won’t size the rows properly.
The View:
import SwiftUI
struct TableViewTest: View {
var body: some View {
TestTableView().frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .center)
}
}
The TableView:
import SwiftUI
struct TestTableView: UIViewRepresentable {
func makeCoordinator() -> Coordinator {
Coordinator()
}
func makeUIView(context: UIViewRepresentableContext<TestTableView>) -> UITableView {
let tableView = UITableView(frame: .zero)
tableView.register(TestTableViewCell.self, forCellReuseIdentifier: "TestTableViewCell")
tableView.rowHeight = UITableView.automaticDimension
let dataSource = UITableViewDiffableDataSource<Section, TestData>(tableView: tableView) { tableView, indexPath, data in
let cell = tableView.dequeueReusableCell(withIdentifier: "TestTableViewCell", for: indexPath) as! TestTableViewCell
cell.data = data
return cell
}
populate(dataSource: dataSource)
context.coordinator.dataSource = dataSource
return tableView
}
func populate(dataSource: UITableViewDiffableDataSource<Section, TestData>) {
let items = [
TestData(color: .red, size: CGSize(width: 40, height: 20)),
TestData(color: .blue, size: CGSize(width: 40, height: 40)),
TestData(color: .green, size: CGSize(width: 40, height: 80))
]
var snapshot = NSDiffableDataSourceSnapshot<Section, TestData>()
snapshot.appendSections([.main])
snapshot.appendItems(items)
dataSource.apply(snapshot)
}
func updateUIView(_ tableView: UITableView, context: UIViewRepresentableContext<TestTableView>) {
guard let dataSource = context.coordinator.dataSource else {
return
}
populate(dataSource: dataSource)
}
class Coordinator {
var dataSource: UITableViewDiffableDataSource<Section, TestData>?
}
enum Section {
case main
}
struct TestData: Hashable {
var id = UUID()
var color: UIColor
var size: CGSize
func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
}
}
The Table View Cell:
import UIKit
class TestTableViewCell: UITableViewCell {
var data: TestTableView.TestData? {
didSet {
if let data = data {
let view = UIView()
view.backgroundColor = data.color
addSubview(view)
// !!! SETTING THE BOTTOM ANCHOR TO NIL OR HEIGHT TO 0 PREVENTS THE TABLE FROM SIZING THE ROWS CORRECTLY
view.anchor(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: nil, paddingTop: 0, paddingLeft: 0, paddingBottom: 0, paddingRight: 0, width: data.size.width, height: data.size.height, enableInsets: false)
}
}
}
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
The UIView Anchor Extension:
func anchor (top: NSLayoutYAxisAnchor?, left: NSLayoutXAxisAnchor?, bottom: NSLayoutYAxisAnchor?, right: NSLayoutXAxisAnchor?, paddingTop: CGFloat, paddingLeft: CGFloat, paddingBottom: CGFloat, paddingRight: CGFloat, width: CGFloat, height: CGFloat, enableInsets: Bool) {
var topInset = CGFloat(0)
var bottomInset = CGFloat(0)
if #available(iOS 11, *), enableInsets {
let insets = self.safeAreaInsets
topInset = insets.top
bottomInset = insets.bottom
}
translatesAutoresizingMaskIntoConstraints = false
if let top = top {
self.topAnchor.constraint(equalTo: top, constant: paddingTop+topInset).isActive = true
}
if let left = left {
self.leftAnchor.constraint(equalTo: left, constant: paddingLeft).isActive = true
}
if let right = right {
rightAnchor.constraint(equalTo: right, constant: -paddingRight).isActive = true
}
if let bottom = bottom {
bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset).isActive = true
}
if height != 0 {
heightAnchor.constraint(equalToConstant: height).isActive = true
}
if width != 0 {
widthAnchor.constraint(equalToConstant: width).isActive = true
}
}
If I anchor only the top left corner and specify a width and height in the table view cell, the table view will not calculate the heights correctly. However, if I also specify the height, the rows will be sized correctly but a warning is generated about a duplicate constraint. Is there a magic combination that allows correct layout but does not produce the warning?
Bad (but no duplicate constraints):
Good (with duplicate constraints):

To fix this issue, you need to change the priority of your bottom constraint and then the warning will go away.
if let bottom = bottom {
let bottomConstraint = bottomAnchor.constraint(equalTo: bottom, constant: -paddingBottom-bottomInset)
bottomConstraint.priority = UILayoutPriority(750)
bottomConstraint.isActive = true
}

Related

Swift: Encapsulated Layout Height always .333 larger than view

I am building a TableView filled with a variable number of cells. One type of those cells should have a fixed height of 69.0, but xcode keeps setting its encapsulated layout height .333 larger than what I set the height to, meaning it breaks the constraints because the encapsulated layout height is 69.333. If I change my Cell's size to 70 for example, the constraint is set to 70.333. I don't understand what causes this.
Here is the Cell:
class AnnotationCell: UITableViewCell {
//Set identifier to be able to call it later on
static let identifier = "AnnotationCell"
var annotation: Annotation!
//MARK: - Configure
public func configure(annotation: Annotation) {
self.annotation = annotation
}
//MARK: - Cell Style
//Add all subviews in here
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
func commonInit() {
//Customize Cell
contentView.backgroundColor = colors.darkGray
contentView.layer.borderWidth = 0
//Favorite
let favoriteView = UIView()
favoriteView.backgroundColor = colors.gray
favoriteView.addBorders(edges: [.top], color: colors.lightGray, width: 1)
favoriteView.translatesAutoresizingMaskIntoConstraints = false
let favIcon = UIImageView()
let myEmoji = "👀".textToImage() //Test
favIcon.image = myEmoji
favIcon.contentMode = .scaleAspectFill
favIcon.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(favIcon)
let stringStack = UIStackView()
stringStack.axis = .vertical
stringStack.spacing = 6
stringStack.translatesAutoresizingMaskIntoConstraints = false
let titleString = UILabel()
titleString.text = "Test"
titleString.textColor = colors.justWhite
titleString.font = UIFont(name: "montserrat", size: 17)
titleString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(titleString)
let addressString = UILabel()
addressString.text = "Test 2"
addressString.textColor = colors.lightGray
addressString.font = UIFont(name: "montserrat", size: 14)
addressString.translatesAutoresizingMaskIntoConstraints = false
stringStack.addArrangedSubview(addressString)
favoriteView.addSubview(stringStack)
let editButton = UIButton()
editButton.tintColor = colors.lightGray
let mediumConfig = UIImage.SymbolConfiguration(pointSize: 20, weight: .semibold, scale: .medium)
let mediumEditButton = UIImage(systemName: "chevron.right", withConfiguration: mediumConfig)
editButton.setImage(mediumEditButton, for: .normal)
editButton.translatesAutoresizingMaskIntoConstraints = false
favoriteView.addSubview(editButton)
contentView.addSubview(favoriteView)
//Define Constraints
NSLayoutConstraint.activate([
favoriteView.heightAnchor.constraint(equalToConstant: 69),
favoriteView.topAnchor.constraint(equalTo: contentView.topAnchor),
favoriteView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
favoriteView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor),
favoriteView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor),
favIcon.leadingAnchor.constraint(equalTo: favoriteView.leadingAnchor, constant: 15),
favIcon.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
favIcon.heightAnchor.constraint(equalToConstant: 50),
favIcon.widthAnchor.constraint(equalToConstant: 50),
stringStack.leadingAnchor.constraint(equalTo: favIcon.trailingAnchor, constant: 20),
stringStack.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
editButton.trailingAnchor.constraint(equalTo: favoriteView.trailingAnchor, constant: -15),
editButton.centerYAnchor.constraint(equalTo: favoriteView.centerYAnchor),
])
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
}
}
I am not setting the Cell's height in the TableView at all - I've tried that and that did not make things better. The Cell is getting displayed correctly, but console throws:
[LayoutConstraints] Unable to simultaneously satisfy constraints.
(
"<NSLayoutConstraint:0x281d21ae0 UIView:0x15cca3490.height == 69 (active)>",
"<NSLayoutConstraint:0x281d21bd0 V:|-(0)-[UIView:0x15cca3490] (active, names: '|':UITableViewCellContentView:0x15cca42a0 )>",
"<NSLayoutConstraint:0x281d22120 UIView:0x15cca3490.bottom == UITableViewCellContentView:0x15cca42a0.bottom (active)>",
"<NSLayoutConstraint:0x281d28820 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x15cca42a0.height == 69.3333 (active)>"
)
Will attempt to recover by breaking constraint
<NSLayoutConstraint:0x281d21ae0 UIView:0x15cca3490.height == 69 (active)>
I have tried giving every subview a fixed height (which does not make sense with a stackview in this layout) but that did not work either. I had the same layout in a view (not TableViewCell) without any issues, so I am guessing that it has to do something with the TableView.
Here are my constraints as seen from the view hierarchy, in case someone has an eye for it. I have opened up most constraints to make it as clear as possible. As can be seen next to the red arrow, the constraint of 69.0 seems to be there and I don't see anything different. The only thing I could see causing this issue would be the "UISysmenBackGroundView" which has a view inside it without any constraints.
1/3 pt sounds like a hairline thickness on a 3x device. Could it perhaps be the separator? If they’re enabled, try turning them off to see if that fixes the issue.

NSCollectionView with vertical and horizontal ability

I am making a list by using NSCollectionView. It may contain many columns and rows. It seems that I can only scroll the view either vertically or horizontally. Sample
I understand the painpoint is that, the width of CollectionView content size is automatically set to the width of documentview in scrollview. But for other kinds of view, such as NSImageView, the size won't be adjusted.
here are the codes
import Cocoa
class ViewController: NSViewController {
override var representedObject: Any? {
didSet {
}
}
var titles = [String]()
var collectionView: NSCollectionView?
override func viewDidLoad() {
super.viewDidLoad()
setup()
}
func setup(){
self.titles = ["Banana", "Apple", "Strawberry", "Cherry", "Pear", "Pineapple", "Grape", "Melon"]
collectionView = NSCollectionView()
collectionView!.itemPrototype = CollectionViewItem()
collectionView!.content = self.titles
collectionView?.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
(collectionView?.widthAnchor.constraint(equalToConstant: 2000))!,
(collectionView?.heightAnchor.constraint(equalToConstant: 2000))!
])
collectionView?.setFrameSize(NSSize(width: 2000, height: 2000))
var index = 0
for title in titles {
var item = self.collectionView!.item(at: index) as! CollectionViewItem
item.getView().button?.title = self.titles[index]
index = index + 1
}
var scrollView = NSScrollView(frame: NSRect(x: 0, y: 0, width: 400 , height: 200))
let clipView = scrollView.contentView
scrollView.backgroundColor = NSColor.red
scrollView.documentView = collectionView
self.view.addSubview(scrollView)
}
}

NSTableView first row initially appears behind the header

I am creating tableView in my application programmatically, and I have this problem:
Whenever the content size of my tableView is bigger than its frame, the first row of tableView appears under the header initially, but it can be easily scrolled back into the position.
The screenshots of what the window looks like after the initial load.
Content size bigger than tableView frame(the issue is here):
Content size smaller than tableView frame(all good here):
My NSViewController class code:
class MainViewController: NSViewController, NSTableViewDelegate, NSTableViewDataSource {
let label: NSTextField = {
let label = NSTextField()
label.stringValue = "Test App"
label.font = NSFont.boldSystemFont(ofSize: 14)
label.textColor = .white
label.alignment = .center
label.drawsBackground = false
label.isBezeled = false
label.isSelectable = true
label.isEditable = false
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
let labelContainerView: NSView = {
let view = NSView()
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
let tableView: NSTableView = {
let tableView = NSTableView()
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.usesAlternatingRowBackgroundColors = true
let column = NSTableColumn(identifier: .init(rawValue: "MyColumnID"))
column.headerCell.title = "Test"
column.headerCell.alignment = .center
tableView.addTableColumn(column)
return tableView
}()
let scrollView: NSScrollView = {
let scrollView = NSScrollView()
scrollView.hasVerticalScroller = true
scrollView.translatesAutoresizingMaskIntoConstraints = false
return scrollView
}()
override func loadView() {
view = NSView()
view.wantsLayer = true
}
var names: [String] = []
override func viewDidLoad() {
super.viewDidLoad()
layoutUI()
for i in 1...9 {
let name = "Test \(i)"
names.append(name)
}
scrollView.documentView = tableView
tableView.delegate = self
tableView.dataSource = self
}
func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? {
let newCell = MyCellView(identfier: NSUserInterfaceItemIdentifier(rawValue: "MyColumnID"))
newCell.textView.stringValue = names[row]
return newCell
}
func numberOfRows(in tableView: NSTableView) -> Int {
return names.count
}
func layoutUI() {
view.addSubview(labelContainerView)
view.addSubview(scrollView)
labelContainerView.addSubview(label)
NSLayoutConstraint.activate([
labelContainerView.topAnchor.constraint(equalTo: view.topAnchor),
labelContainerView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
labelContainerView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
labelContainerView.heightAnchor.constraint(equalTo: view.heightAnchor,
multiplier: 1/5),
label.centerXAnchor.constraint(equalTo: labelContainerView.centerXAnchor),
label.centerYAnchor.constraint(equalTo: labelContainerView.centerYAnchor,
constant: -4),
scrollView.heightAnchor.constraint(equalTo: view.heightAnchor,
multiplier: 4/5),
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor)
])
}
}
I would not recommend to setup a NSTableView in code. Its much easier to do this in Interface Builder and may prevent unwanted side effects. In any case if NSTableView is not displaying the first row as expected I would use scrollToVisible on the related NSScrollView to make the first row visible after loading the data.
I've been working on the same problem (programmatically creating scrolling tables). I built a breadboard version, which worked except for this particular problem.
When an NSTableView has an NSTableHeaderView, the NSScrollview actually has TWO NSClipviews. The one accessed from the .contentView property contains the table view, the 2nd contains the header and an NSVisualEffectView. They are both subviews of the scroll view.
I got it to behave, that is, show the top row, by adjusting the contentView insets:
scrollView.contentView.contentInsets = NSEdgeInsets(top: headerView!.frame.height, left: 0, bottom: 0, right: 0)
I tried various auto layout strategies, which didn't work, or seemed dubious.
Here is a trace of what it looks like for a two-column table filled with generated data to let me see what's going on.
Overview (not code, but the only way I could get it to format):
Scroll View Subviews: 4
NSClipView
NSTableBackgroundView (-250.0, -456.0, 1000.0, 456.0)
NSTableView (0.0, 0.0, 500.0, 4750.0)
NSClipView
NSVisualEffectView (0.0, 0.0, 500.0, 23.0)
NSTableHeaderView (0.0, 0.0, 500.0, 23.0)
NSScroller
NSScroller
The two clip views, with their frame rects. 0 is the content view:
ClipView 0 (0.0, 0.0, 500.0, 438.0)
NSTableBackgroundView 0 (-250.0, -456.0, 1000.0, 456.0)
NSTableView 1 (0.0, 0.0, 500.0, 4750.0)
ClipView 1 (0.0, 0.0, 500.0, 23.0)
NSVisualEffectView 0 (0.0, 0.0, 500.0, 23.0)
NSTableHeaderView 1 (0.0, 0.0, 500.0, 23.0)
Still don't completely understand what's going on.
But I do believe that knowing how this stuff works under the hood, without IB magic, is a worthwhile exercise.

Swift - TextView that expands and stays in center

I am trying to accomplish an Idea I got in my head but I got stuck..
I need a TextView that expands in both ways: widht-height.
That has a minimum and maximum width, and minimum height.
That is centered in the middle of the parent (SCROLL) view.
And that has a button send at the bottom trailing part of the view.
Here's the idea:
So if the user types in the box then it expands in both directions. But there's a maximum width for it (so it doesn't go offscreen) but the height is not limited: due to the parent scrollview.
The problem is that the textView's height doesn't expand when text is breaking into new line.
Code:
func textViewDidChange(_ textView: UITextView) {
self.adjustTextViewFrames(textView: textView)
}
func adjustTextViewFrames(textView : UITextView){
var newSize = textView.sizeThatFits(CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude))
if newSize.width > self.view.bounds.width - 20 {
newSize.width = self.view.bounds.width - (self.view.bounds.width/10)
}
messageBubbleTextViewWidthConstraint.constant = newSize.width
messageBubbleTextViewHeightConstraint.constant = newSize.height
UIView.animate(withDuration: 0.3) {
self.view.layoutIfNeeded()
}
}
Try This :
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// let's create our text view
let textView = UITextView()
textView.frame = CGRect(x: 0, y: 0, width: 200, height: 100)
textView.backgroundColor = .lightGray
textView.text = "Here is some default text that we want to show and it might be a couple of lines that are word wrapped"
view.addSubview(textView)
// use auto layout to set my textview frame...kinda
textView.translatesAutoresizingMaskIntoConstraints = false
[
textView.bottomAnchor.constraint(equalTo: view.safeAreaLayoutGuide.bottomAnchor),
textView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
textView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
textView.heightAnchor.constraint(equalToConstant: 50)
].forEach{ $0.isActive = true }
textView.font = UIFont.preferredFont(forTextStyle: .headline)
textView.delegate = self
textView.isScrollEnabled = false
textViewDidChange(textView)
}
}
extension ViewController: UITextViewDelegate {
func textViewDidChange(_ textView: UITextView) {
print(textView.text)
let size = CGSize(width: view.frame.width, height: .infinity)
let estimatedSize = textView.sizeThatFits(size)
textView.constraints.forEach { (constraint) in
if constraint.firstAttribute == .height {
constraint.constant = estimatedSize.height
}
}
}
}
Credit goes to Brian Voong from Let's Build That App here link

Can you get a UITableView's intrinsic content size to update based on the number of rows shown if scrolling is disabled?

We have a portion of our UI which is a small list of labels with color swatches next to them. The design I'm taking over has six of these hard-coded in the layout even though the actual data is dynamic, meaning if we only need to show three, we have to explicitly hide three, which also throws off the balance of the page. Making matters worse is each one of those 'items' is actually made up of several sub-views so a screen with six hard-coded items has eighteen IBOutlets.
What I'm trying to do is to instead use a UITableView to represent this small portion of the screen, and since it won't scroll, I was wondering if you can use AutoLayout to configure the intrinsic content height of the UITableView to be based on the number of rows.
Currently I have a test page with a UITableView vertically constrained to the center, but without a height constraint because I am hoping to have the table's intrinsic content size reflect the visible rows. I have also disabled scrolling on the table. When I reload the table, I call updateConstraints. But the table still does not resize.
Note: We can't use a UIStackView (which would have been perfect for this) because we have to target iOS8 and that wasn't introduced until iOS9, hence this solution.
Has anyone been able to do something similar to our needs?
Ok, so unlike UITextView, it doesn't look like UITableView ever returns an intrinsic size based on the visible rows. But that's not that big a deal to implement via a subclass, especially if there's a single section, no headers or footers, and the rows are of a fixed height.
class AutoSizingUiTableView : UITableView
{
override func intrinsicContentSize() -> CGSize
{
let requiredHeight = rowCount * rowHeight
return CGSize(width: UIView.noIntrinsicMetric, height: CGFloat(requiredHeight))
}
}
I'll leave it up to the reader to figure out how to get their own rowCount. The same if you have variable heights, multiple sections, etc. You just need more logic.
By doing this, it works great with AutoLayout. I just wish it handled this automatically.
// Define this puppy:
class AutoTableView: UITableView {
override func layoutSubviews() {
super.layoutSubviews()
self.invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: CGSize {
get {
var height:CGFloat = 0;
for s in 0..<self.numberOfSections {
let nRowsSection = self.numberOfRows(inSection: s)
for r in 0..<nRowsSection {
height += self.rectForRow(at: IndexPath(row: r, section: s)).size.height;
}
}
return CGSize(width: UIView.noIntrinsicMetric, height: height)
}
set {
}
}
}
and make it your class in IB.
obs: this is if your class is only cells and shit. if it has header, footer or some other thign, dunno. it'll not work. for my purposes it works
peace
This can be done, please see below for a very simple (and rough - rotation does not work properly!) example, which allows you to update the size of the table view by entering a number in the text field and resetting with a button.
import UIKit
class ViewController: UIViewController {
var tableViewController : FlexibleTableViewController!
var textView : UITextView!
var button : UIButton!
var count : Int! {
didSet {
self.refreshDataSource()
}
}
var dataSource : [Int]!
let rowHeight : CGFloat = 50
override func viewDidLoad() {
super.viewDidLoad()
// Configure
self.tableViewController = FlexibleTableViewController(style: UITableViewStyle.plain)
self.count = 10
self.tableViewController.tableView.backgroundColor = UIColor.red
self.textView = UITextView()
self.textView.textAlignment = NSTextAlignment.center
self.textView.textColor = UIColor.white
self.textView.backgroundColor = UIColor.blue
self.button = UIButton()
self.button.setTitle("Reset", for: UIControlState.normal)
self.button.setTitleColor(UIColor.white, for: UIControlState.normal)
self.button.backgroundColor = UIColor.red
self.button.addTarget(self, action: #selector(self.updateTable), for: UIControlEvents.touchUpInside)
self.layoutFrames()
// Assemble
self.view.addSubview(self.tableViewController.tableView)
self.view.addSubview(self.textView)
self.view.addSubview(self.button)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func refreshDataSource() -> Void {
if let _ = self.dataSource {
if !self.dataSource.isEmpty {
self.dataSource.removeAll()
}
}
else
{
self.dataSource = [Int]()
}
for count in 0..<self.count {
self.dataSource.append(count)
}
self.tableViewController.dataSource = self.dataSource
self.tableViewController.tableView.reloadData()
if let _ = self.view {
self.layoutFrames()
self.view.setNeedsDisplay()
}
}
func updateTable() -> Void {
guard let _ = self.textView.text else { return }
guard let validNumber = Int(self.textView.text!) else { return }
self.count = validNumber
}
func layoutFrames() -> Void {
if self.tableViewController.tableView != nil {
self.tableViewController.tableView.frame = CGRect(origin: CGPoint(x: self.view.frame.width / 2 - 100, y: 100), size: CGSize(width: 200, height: CGFloat(self.dataSource.count) * self.rowHeight))
NSLog("\(self.tableViewController.tableView.frame)")
}
if self.textView != nil {
self.textView.frame = CGRect(origin: CGPoint(x: 50, y: 100), size: CGSize(width: 100, height: 100))
}
if self.button != nil {
self.button.frame = CGRect(origin: CGPoint(x: 50, y: 150), size: CGSize(width: 100, height: 100))
}
}
}
class FlexibleTableViewController : UITableViewController {
var dataSource : [Int]!
override init(style: UITableViewStyle) {
super.init(style: style)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.dataSource.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
self.tableView.register(UITableViewCell.self, forCellReuseIdentifier: "cell")
let cell = tableView.dequeueReusableCell(withIdentifier: "cell") ?? UITableViewCell()
cell.frame = CGRect(origin: CGPoint(x: 10, y: 5), size: CGSize(width: 180, height : 40))
cell.backgroundColor = UIColor.green
return cell
}
}
Whether it is a good idea or not, is, as has been pointed out, another question! Hope that helps!
Version from no_ripcord accounting for header and footer height
final // until proven otherwise
class IntrinsicallySizedTableView: UITableView {
override func layoutSubviews() {
super.layoutSubviews()
self.invalidateIntrinsicContentSize()
}
override var intrinsicContentSize: CGSize {
guard let dataSource = self.dataSource else {
return super.intrinsicContentSize
}
var height: CGFloat = (tableHeaderView?.intrinsicContentSize.height ?? 0)
+ contentInset.top + contentInset.bottom
if let footer = tableFooterView {
height += footer.intrinsicContentSize.height
}
let nsections = dataSource.numberOfSections?(in: self) ?? self.numberOfSections
for section in 0..<nsections {
let sectionheader = rectForHeader(inSection: section)
height += sectionheader.height
let sectionfooter = rectForFooter(inSection: section)
height += sectionfooter.height
let nRowsSection = self.numberOfRows(inSection: section)
for row in 0..<nRowsSection {
height += self.rectForRow(at: IndexPath(row: row, section: section)).size.height
}
}
return CGSize(width: UIView.noIntrinsicMetric, height: height)
}
}