2016-07-26 3 views
1

テーブルビューでヘッダーセクションのタイトルの垂直方向の配置を変更する:スウィフト - 私は私の関数のヘッダを移入するには、次のコードを使用しています

func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? { 

    switch section { 

    case 0: "SECTION 0" 

    case 1: "SECTION 1" 

    default: break 

    } 

    return nil 
} 

func tableView(tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) 
{ 
    var title = UILabel() 
    title.font = UIFont(name: "HelveticaNeue-Light", size: 12)! 
    title.textColor = UIColor.blackColor() 

    let header = view as! UITableViewHeaderFooterView 
    header.textLabel?.font=title.font 
    header.textLabel?.textColor=title.textColor 
    header.backgroundView?.backgroundColor = UIColor.whiteColor() 

} 

、私は垂直方向を変更できるようにしたいと思います以下の写真のようなセクション内のタイトルの配置、:

Changing the vertical alignment of the title in the section of the TableView

は、私はそれをどのように行うことができますか?

答えて

2

残念ながら、UILabelのテキストを縦に並べる方法はありません。 This SOの投稿がさらに詳細になります。

しかし親のUIViewにラベルを追加し、そのラベルを下に拘束することで同様の効果を得ることができます。その後、そのビューをviewForHeaderInSectionテーブルビューで返すことができます。データソースメソッド

let headerView = UILabel(frame: CGRect(origin: CGPointZero, size: CGSize(width: self.view.frame.width, height: 50))) 
let label = UILabel(frame: CGRect(x: 0, y: 20, width: self.view.frame.width, height: 30)) 
label.text = "Some Text" 
headerView.addSubview(label) 
return headerView 
+0

パーフェクト!私は使用していた2つの関数を削除しましたが、今では関数 "func tableView(tableView:UITableView、viewForHeaderInSectionセクション:Int) - > UIViewの中で自分のコードを使用しています。ありがとうございました! –

+0

助けてうれしい! – Lahav

関連する問題