Created
December 14, 2018 13:21
-
-
Save t089/51037207176b60f53fc5367c47d2abec to your computer and use it in GitHub Desktop.
UITableViewController+AutoLayoutHeaderView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// ViewController.swift | |
// NaceApp | |
// | |
// Created by Tobias Haeberle on 14.12.18. | |
// | |
import UIKit | |
class ViewController: UITableViewController { | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
// Do any additional setup after loading the view, typically from a nib. | |
self.updateLabelText() | |
} | |
@IBOutlet weak var headerView: UIView! | |
@IBOutlet weak var label: UILabel! | |
func updateLabelText() { | |
let labelText = (0..<Int.random(in: 1..<50)).reduce(String(), { str, i in return str + " my label " }) | |
self.label.text = labelText | |
let size = self.headerView.systemLayoutSizeFitting(CGSize(width: self.tableView.frame.width, height: .greatestFiniteMagnitude), withHorizontalFittingPriority: .required, verticalFittingPriority: .defaultLow) | |
self.headerView.bounds.size = size | |
self.tableView.tableHeaderView = self.headerView | |
// self.tableView.setNeedsLayout() | |
// self.tableView.layoutIfNeeded() | |
DispatchQueue.main.asyncAfter(deadline: .now() + 1, execute: { | |
self.updateLabelText() | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment