我有TableViewController
标题。
此标头是一个容器,它链接到另一个名为Header.storyboard
Header.storyboard
包含带有一些标签的堆栈视图,这些标签是动态类型的。
标签文字来自数据库。
不考虑文本维度或大小我想要正确地将其可视化。
我使用了SO的一些答案来正确调整标题大小,但没有运气:
import UIKit
class TableViewController: UITableViewController {
@IBOutlet weak var tableHeaderView: UIView!
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Dynamic sizing for the header view
if let headerView = tableHeaderView {
headerView.setNeedsLayout()
headerView.layoutIfNeeded()
let height = headerView.systemLayoutSizeFitting(UILayoutFittingCompressedSize).height
var headerFrame = headerView.frame
// If we don't have this check, viewDidLayoutSubviews() will get
// repeatedly, causing the app to hang.
if height != headerFrame.size.height {
headerFrame.size.height = height
headerView.frame = headerFrame
tableHeaderView = headerView
}
}
}
}
答案 0 :(得分:3)
首先,您应该为标签指定高度
,而不是给视图赋予高度class ViewController : UITableViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 40
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 4
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
cell.backgroundColor = UIColor.yellow
cell.textLabel?.text = "Baran"
return cell
}
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat
{
return heightForView()
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let headerView : UIView = UIView(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: self.heightForView()))
headerView.backgroundColor = UIColor.black
let label : UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.heightForView()))
label.numberOfLines = 0
label.textAlignment = NSTextAlignment.center
label.text = "Size To Fit Tutorial"
label.font = UIFont(name: "Helvetica", size: 50)
label.textColor = UIColor.white
headerView.addSubview(label)
return headerView
}
//Self Sizing height ....
func heightForLabel(text:String, font:UIFont, width:CGFloat) -> CGFloat{
let label:UILabel = UILabel(frame: CGRect(x: 0, y: 0, width: width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.lineBreakMode = NSLineBreakMode.byCharWrapping
label.font = font
label.text = text
label.sizeToFit()
return label.frame.height
}
func heightForView() -> CGFloat{
let screenSize = UIScreen.main.bounds
let screenWidth = screenSize.width
let text = "Size To Fit Tutorial"
let font : UIFont!
switch UIDevice.current.userInterfaceIdiom {
case .pad:
font = UIFont(name: "Helvetica", size: 35)
case .phone:
font = UIFont(name: "Helvetica", size: 50)
default:
font = UIFont(name: "Helvetica", size: 24)
}
let height = heightForLabel(text: text, font: font, width: screenWidth)
return height
}
}
希望这会有所帮助
答案 1 :(得分:3)
表视图标题可以自动调整大小
但是使用容器可能无法正常工作
使用自定义视图代替viewForHeaderInSection
class ViewController: UITableViewController {
override func viewDidLoad() {
tableView.estimatedSectionHeaderHeight = 10
tableView.sectionHeaderHeight = UITableViewAutomaticDimension
}
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
let lab = UILabel()
lab.text = "text \(section)"
lab.font = UIFont.systemFont(ofSize: 10 * CGFloat(section) + 1)
return lab
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 5
}
//this method overriding is not necessary
override func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return UITableViewAutomaticDimension
}
}
答案 2 :(得分:2)
尝试实施
tableView(_ tableView: UITableView, heightForHeaderInSection section: Int)
和
tableView(_ tableView: UITableView, viewForHeaderInSection section: Int)
您的代码应如下所示:
func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
return tableHeaderView.bounds.size.height
}
func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableHeaderView
}
答案 3 :(得分:0)