Last active
June 11, 2020 06:56
-
-
Save egzonpllana/65bb2ae28d86fba7835e28e9e35c872d to your computer and use it in GitHub Desktop.
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
// | |
// Created by Egzon Pllana on 5/15/20. | |
// Copyright © 2020 Native Coders. All rights reserved. | |
// | |
import UIKit | |
class HomeTableViewController: UITableViewController { | |
// MARK: - Outlets | |
@IBOutlet weak var commentTextView: UITextView! | |
// MARK: - View lifecycle | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
/* Dynamic height with UITextView | |
// commentTextView should not have height constraint | |
// commentTextView must have top and bottom constraint connected to cell view | |
// Disable textView scroll to enable auto layout | |
*/ | |
tableView.estimatedRowHeight = 150 | |
commentTextView.isScrollEnabled = false | |
} | |
} | |
// MARK: - Table view data source | |
extension HomeTableViewController { | |
override func numberOfSections(in tableView: UITableView) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { | |
return 1 | |
} | |
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { | |
return UITableView.automaticDimension | |
} | |
} | |
// MARK: - Text view delegates | |
extension HomeTableViewController: UITextViewDelegate { | |
func textViewDidChange(_ textView: UITextView) { | |
// Refresh tableView cell | |
// Animated height update | |
DispatchQueue.main.async { | |
self.tableView?.beginUpdates() | |
self.tableView?.endUpdates() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment