2017-03-01 28 views
9

我有一个带有标题的TableViewController。 此标头是一个容器,它连接到名为Header.storyboard使用动态类型时调整表格标题的大小

TableViewController

Header.storyboard包含具有一些标签,即是动态类型的叠层视图另一情节串连图板。

标签文本来自DB。

对文字尺寸或尺寸没有问题我想正确显示它。

enter image description here

我用从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

你试过FUNC的tableView(UITableView的,heightForHeaderInSection:智力)中的UITableViewDelegate? – RinaLiu

+0

您的意思是它使用动态类型作为Apple技术(例如UIFontTextStyle),或者您的意思是它们只是具有不同的字体大小? – Alistra

+0

@Alistra,我的意思是苹果定义它的动态类型 - 'storyboard'中使用的UIFontTextStyle。当应用程序运行时,我想从可访问性更改字体,并且我希望相应地更改tableHeader的高度,以使文本可见 –

回答

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 
} 
+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 
} 
} 

enter image description here

希望这有助于

+0

没有我写的代码工作@ Laura Calinoiu –

+0

这不是我想要的,我想退出应用程序,并从设置中更改字体,并且我需要自动调整大小的标题,以便在使用更大的字体时这应该是可见的。在代码中不能改变太多,就像你建议的那样,因为我使用的是遗留代码。另外,我的头文件位于一个容器中,因为那里有大逻辑,所以我不想改变它。谢谢你的提问和回答 –

2

表视图标题可以自动调整大小

但随着集装箱很可能将无法正常工作

使用自定义视图为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 
    } 
} 

Result

+0

我可以不要删除容器,因为它是遗留代码 –

+0

我的代码中的'viewDidLayoutSubviews'实际上会自动调整容器的大小,但它似乎并不在意字体是否已更改 –

+0

autoLayout aut oheigh与容器,嵌入式viewControllers和其他simular东西非常不好。我看到的唯一方法是:通过systemLayoutSizeFitting以编程方式计算高度并将其传递给heightForHeaderInSection – Andrew