2017-12-16 23 views
2

私はいくつかの問題があります。私はカスタムテーブルヘッダーセクションを作成します。ここに私のコードです:親ビューでテキストラベルの高さを管理する方法

func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
     let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 36)) 

     sectionHeader.backgroundColor = #colorLiteral(red: 0.1215686277, green: 0.01176470611, blue: 0.4235294163, alpha: 1) 

     tableView.backgroundColor = colors.background 

     let headerName = UILabel(frame: CGRect(x: 15, y: 0, width: tableView.frame.size.width, height: sectionHeader.frame.size.height)) 

     headerName.font = UIFont(name: "Helvetica", size: 12) 
     headerName.text = sectionHeaders[section] 
     headerName.backgroundColor = #colorLiteral(red: 0.9529411793, green: 0.6862745285, blue: 0.1333333403, alpha: 1) 
     headerName.clipsToBounds = true 

     sectionHeader.addSubview(headerName) 

     return sectionHeader 
    } 

そして、ここでの結果である:私はマークされ、気づいた青と黄色、headerviewと同じではない、そのラベルの高さを持つ enter image description here

。いくつかのいずれかのビューの高さをヘッダにtextlabel高さを管理する方法、およびテキストにheaderviewと同じではありませんverticaly

+0

あなたは '使用のfuncのtableViewしたテキストを整列させる(_のtableView:のUITableView、heightForHeaderInSectionセクション:INT)'? –

答えて

3

ラベルの高さを揃えるためにどのように私を助けることができるまず

あなたべきあなたのセクションヘッダの設定高さ:

func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat { 
    return 36.0 
} 

let sectionHeader = UIView(frame: CGRect(x: 0, y: 0, width: tableView.frame.size.width, height: 36))

から height: 36に基づいて、

はどのようにverticaly

headerName.textAlignment = .center 
+0

ありがとう!できます! – WhoIsDT

+0

@WhoIsDTも更新をチェックしてください –

関連する問題