UITableView Header section height is not adjusted automatically for a Custom UIView() class

UITableView Header section height is not adjusted automatically for a Custom UIView() class

I am having UIView() class where I am adding a label programatically and also given constraints to automatically adjust height of view based on content. I have used this class as HeaderView for a UItableView section. But the problem here is the height of this view is not adjusting accordingly to its content.

I am having UIView() class where I am adding a label programatically and also given constraints to automatically adjust height of view based on content. I have used this class as HeaderView for a UItableView section. But the problem here is the height of this view is not adjusting accordingly to its content.

Here he is my code of that custom View.

class DynamicHeaderView: UIView {

override func draw(_ rect: CGRect) { let headerLabel = UILabel() headerLabel.numberOfLines = 0 headerLabel.sizeToFit() headerLabel.text = "This is header view. It is dynamicaaly growing text and will automaticaly get adjusted to it" self.backgroundColor = .green headerLabel.translatesAutoresizingMaskIntoConstraints = false

self.addSubview(headerLabel)

self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .leading, relatedBy: .equal, toItem: self, attribute: .leading, multiplier: 1, constant: 16))

self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .trailing, relatedBy: .equal, toItem: self, attribute: .trailing, multiplier: 1, constant: -16))

self.addConstraint(NSLayoutConstraint(item: headerLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1, constant: 10))

} }

Code that I have written in my viewController,

 override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    self.countriesTable.sectionHeaderHeight = UITableView.automaticDimension;
    self.countriesTable.estimatedSectionHeaderHeight = 25 }

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { let headerView = DynamicHeaderView() return headerView }

The height is always stick to the estimated header height as 25 which i have given in viewDidLoad() function.

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

iOS App Development: How Apple's iOS 13 & Swift 5 Will Change App Development Industry

Want to know how Apple's iOS 13 & Swift 5 will change the iOS app development industry? Refer to this blog that contains in-detail information on it.

How To Build Reminders iOS App in Swift 5 for Beginners

In this video, we build the Reminders App in Swift for iOS. Learn how to use dates, local / user notifications, tableviews, and much more. From scratch to working app, we'll go over every step up toe process together.

Creating an iOS app with user presence using Node.js and Swift

In this tutorial, we are going to see how you can add user presence to an iOS application using Pusher Channels, Node.js, and Swift.