Created
September 10, 2021 17:54
-
-
Save denandreychuk/228b0cd81dcf88a88bb451c7ec2964ec to your computer and use it in GitHub Desktop.
Как использовать tableHeaderView вместе с auto-layout? | https://t.me/BlogSwift
This file contains hidden or 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
| // СВИФТЕР | Блог про Swift | t.me/BlogSwift | |
| extension UITableView { | |
| func setTableHeaderView(headerView: UIView) { | |
| tableHeaderView = headerView | |
| headerView.translatesAutoresizingMaskIntoConstraints = false | |
| headerView.widthAnchor.constraint(equalTo: self.widthAnchor).isActive = true | |
| headerView.centerXAnchor.constraint(equalTo: self.centerXAnchor).isActive = true | |
| headerView.topAnchor.constraint(equalTo: self.topAnchor).isActive = true | |
| } | |
| func updateTableHeaderViewIfNeeded() { | |
| guard let headerView = tableHeaderView else { return } | |
| let oldSize = headerView.bounds.size | |
| headerView.layoutIfNeeded() | |
| let newSize = headerView.bounds.size | |
| guard oldSize != newSize else { return } | |
| beginUpdates() | |
| endUpdates() | |
| } | |
| } | |
| // Внутри ViewController | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| tableView.setTableHeaderView(headerView: someHeaderView) | |
| } | |
| override func viewDidLayoutSubviews() { | |
| super.viewDidLayoutSubviews() | |
| tableView.updateTableHeaderViewIfNeeded() | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment