Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 348 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Resizing UITableView to fit content

#11
objc version of Musa almatri

(void)viewWillLayoutSubviews
{
[super updateViewConstraints];
CGFloat desiredHeight = self.tableView.contentSize.height;
// clamp desired height, if needed, and, in that case, leave scroll Enabled
self.tableHeight.constant = desiredHeight;
self.tableView.scrollEnabled = NO;
}

Reply

#12
I had a table view inside scroll view and had to calculate tableView's height and resize it accordingly. Those are steps I've taken:

0) add a UIView to your scrollView (probably will work without this step but i did it to avoid any possible conflicts) - this will be a containr view for your table view. If you take this step , then set the views borders right to tableview's ones.

1) create a subclass of UITableView:

class IntrinsicTableView: UITableView {

override var contentSize:CGSize {
didSet {
self.invalidateIntrinsicContentSize()
}
}

override var intrinsicContentSize: CGSize {
self.layoutIfNeeded()
return CGSize(width: UIViewNoIntrinsicMetric, height: contentSize.height)
}

}

2) set class of a table view in Storyboard to IntrinsicTableView: screenshot:

[To see links please register here]


3) Set the heightConstraint to your table view

4) drag the IBoutlet of your table to your ViewController

5) drag the IBoutlet of your table's height constraint to your ViewController

6) add this method into your ViewController:

override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.yourTableViewsHeightConstraint?.constant = self.yourTableView.intrinsicContentSize.height
}


Hope this helps


Reply

#13
You can try Out this Custom `AGTableView`

To Set a TableView Height Constraint Using storyboard or programmatically. (This class automatically fetch a height constraint and set content view height to yourtableview height).

class AGTableView: UITableView {

fileprivate var heightConstraint: NSLayoutConstraint!

override init(frame: CGRect, style: UITableViewStyle) {
super.init(frame: frame, style: style)
self.associateConstraints()
}

required public init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.associateConstraints()
}

override open func layoutSubviews() {
super.layoutSubviews()

if self.heightConstraint != nil {
self.heightConstraint.constant = self.contentSize.height
}
else{
self.sizeToFit()
print("Set a heightConstraint to Resizing UITableView to fit content")
}
}

func associateConstraints() {
// iterate through height constraints and identify

for constraint: NSLayoutConstraint in constraints {
if constraint.firstAttribute == .height {
if constraint.relation == .equal {
heightConstraint = constraint
}
}
}
}
}

**Note** If any problem to set a Height then `yourTableView.layoutSubviews()`.
Reply

#14
If you want your table to be dynamic, you will need to use a solution based on the table contents as detailed above. If you simply want to display a smaller table, you can use a container view and embed a UITableViewController in it - the UITableView will be resized according to the container size.

This avoids a lot of calculations and calls to layout.
Reply

#15
My Swift 5 implementation is to set the hight constraint of the tableView to the size of its content (`contentSize.height`). This method assumes you are using auto layout. This code should be placed inside the [`cellForRowAt`](

[To see links please register here]

) tableView method.

``` swift
tableView.heightAnchor.constraint(equalToConstant: tableView.contentSize.height).isActive = true
```


Reply

#16
In case your contentSize is not correct this is because it is based on the estimatedRowHeight (automatic), use this before

> tableView.estimatedRowHeight = 0;


source :

[To see links please register here]

Reply

#17
**Swift Solution**

Follow these steps:

1. Set the height constraint for the table from the storyboard.

2. Drag the height constraint from the storyboard and create `@IBOutlet` for it in the view controller file.

```swift
@IBOutlet var tableHeight: NSLayoutConstraint!
```

3. Then you can change the height for the table dynamicaly using this code:

```swift
override func viewWillLayoutSubviews() {
super.updateViewConstraints()
self.tableHeight?.constant = self.table.contentSize.height
}
```

If the last row is cut off, try to call `viewWillLayoutSubviews()` in `willDisplay cell` function:

```swift
func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
self.viewWillLayoutSubviews()
}
```
Reply

#18
**Swift 5 Solution**

Follow these four steps:

1. Set the height constraint for the tableview from the storyboard.

2. Drag the height constraint from the storyboard and create `@IBOutlet` for it in the view controller file.

```swift
@IBOutlet var tableViewHeightConstraint: NSLayoutConstraint!
```
3. Add an observer for the contentSize property on the ``` override func viewDidLoad() ```
```swift
override func viewDidLoad() {
super.viewDidLoad()
self.tableView.addObserver(self, forKeyPath: "contentSize", options: .new, context: nil)

}

```

4. Then you can change the height for the table dynamicaly using this code:

```swift
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
if(keyPath == "contentSize"){
if let newvalue = change?[.newKey]
{
DispatchQueue.main.async {
let newsize = newvalue as! CGSize
self.tableViewHeightConstraint.constant = newsize.height
}

}
}
}
```

Reply

#19
I am using a **UIView extension** , approach is close to @ChrisB approach above

extension UIView {
func updateHeight(_ height:NSLayoutConstraint)
{

let newSize = CGSize(width: self.frame.size.width, height: CGFloat(MAXFLOAT))
let fitSize : CGSize = self.sizeThatFits(newSize)

height.constant = fitSize.height


}
}

**implementation :** :

@IBOutlet weak var myTableView: UITableView!
@IBOutlet weak var myTableVieweHeight: NSLayoutConstraint!
//(call it whenever tableView is updated inside/outside delegate methods)
myTableView.updateHeight(myTableVieweHeigh)
**Bonus** : Can be used on any other UIViews eg:your own dynamic label
Reply

#20
**based on**
[fl034's answer][1]

**SWift 5**


var tableViewHeight: NSLayoutConstraint?

tableViewHeight = NSLayoutConstraint(item: servicesTableView,
attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute,
multiplier: 0.0, constant: 10)
tableViewHeight?.isActive = true


func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
tableViewHeight?.constant = tableView.contentSize.height
tableView.layoutIfNeeded()
}


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through