//
// © 2017-2020 Qualcomm Technologies, Inc. and/or its subsidiaries. All rights reserved.
//

import UIKit

class WiFiAdviceTableViewController: UITableViewController {

    // Next two methods are to make auto layout magic work by resizing the header height to fit the text with
    // appropriate margins.
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        sizeHeaderToFit()
    }
    
    func sizeHeaderToFit() {
        let headerView = tableView.tableHeaderView!
        
        headerView.setNeedsLayout()
        headerView.layoutIfNeeded()
        
        let height = headerView.systemLayoutSizeFitting(UIView.layoutFittingCompressedSize).height
        
        var frame = headerView.frame
        let oldHeight = frame.size.height
        
        if abs(oldHeight-height) > 0.01 {
            // We only update if it's changed otherwise we enter infinite loop
            frame.size.height = height
            headerView.frame = frame
            tableView.tableHeaderView = headerView
        }
    }
}
