2015-01-10 5 views
5

TableViewのセクションヘッダーの右側にUIButtonをフロートさせることを全面的に揃えようとしています。 は、これまでのところ、私は唯一の画面サイズの制約を追加するために管理...ここテーブルビューセクションのメイクボタンが浮動小数点/プログラムに即座に整列// Swift

は、これまでの私のコードです:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    var headerFrame:CGRect = tableView.frame 

    let titelArr: NSArray = ["1", "2", "3", "4", "5", "6"] 
    var title = UILabel(frame: CGRectMake(10, 10, 100, 30)) 
    title.font = UIFont.boldSystemFontOfSize(20.0) 
    title.text = titelArr.objectAtIndex(section) as? String 
    title.textColor = UIColor.whiteColor() 


    var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.ContactAdd) as UIButton 
    headBttn.frame = CGRectMake(320, 10, 30, 30) 
    headBttn.enabled = true 
    headBttn.titleLabel?.text = title.text 
    headBttn.tag = titelArr.indexOfObject(titelArr.objectAtIndex(section)) 
    headBttn.addTarget(self, action: "addItem:", forControlEvents: UIControlEvents.TouchUpInside) 

    var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height)) 
    headerView.backgroundColor = UIColor(red: 108/255, green: 185/255, blue: 0/255, alpha: 0.9) 
    headerView.addSubview(title) 
    headerView.addSubview(headBttn) 

    return headerView 

} 

は、どのように私は、ボタンを右浮くことができますか?残りの制約条件はそのままで...

THXあなたの助けに! //セブ

+0

「フロート・ライト」とはどういう意味ですか? 「1画面サイズの制約を追加しましたが、制約は一切追加していません。任意の制約を追加するコードはありません(制約によってはNSLayoutConstraintsを意味します)。 – rdelmar

+0

申し訳ありませんが、私は迅速に新しいです!実際には、プログラムで制約を設定する方法はわかりません。私はボタンがすべてのデバイスのセクションヘッダーの右側にあるようにします。 'CGRectMake(320、10、30、30) 'の上にあるコードでは、ボタンはiPhone 6の肖像画の右側にある固定位置にあります。 ;-) – Seb

+0

これを行う方法がわからない場合は、NSLayoutConstraintクラスリファレンスを読んでください。これには、プログラムで制約を追加するために必要なswiftメソッドとobjective-Cメソッドが含まれています。コードを記述しようとする必要があります。つまった場合は、より具体的な質問に戻ってください。 – rdelmar

答えて

8

Thxを

は@rdelmarすると誰もが驚くべき回答@Sebを共有するための;-)

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? { 
    var headerFrame = tableView.frame 

    var headerView:UIView = UIView(frame: CGRectMake(0, 0, headerFrame.size.width, headerFrame.size.height)) 
    headerView.backgroundColor = UIColor(red: 108/255, green: 185/255, blue: 0/255, alpha: 0.9) 

    var title = UILabel() 
    title.setTranslatesAutoresizingMaskIntoConstraints(false) 
    title.font = UIFont.boldSystemFontOfSize(20.0) 
    title.text = titelArr.objectAtIndex(section) as? String 
    title.textColor = UIColor.whiteColor() 
    headerView.addSubview(title) 

    var headBttn:UIButton = UIButton.buttonWithType(UIButtonType.System) as UIButton 
    headBttn.setTranslatesAutoresizingMaskIntoConstraints(false) 
    headBttn.enabled = true 
    headBttn.titleLabel?.text = title.text 
    headBttn.tag = titelArr.indexOfObject(titelArr.objectAtIndex(section)) 
    headBttn.addTarget(self, action: "addItem:", forControlEvents: UIControlEvents.TouchUpInside) 
    headerView.addSubview(headBttn) 

    var viewsDict = Dictionary <String, UIView>() 
    viewsDict["title"] = title 
    viewsDict["headBttn"] = headBttn 

    headerView.addConstraints(
     NSLayoutConstraint.constraintsWithVisualFormat(
      "H:|-10-[title]-[headBttn]-15-|", options: nil, metrics: nil, views: viewsDict)) 

    headerView.addConstraints(
     NSLayoutConstraint.constraintsWithVisualFormat(
      "V:|-[title]-|", options: nil, metrics: nil, views: viewsDict)) 
    headerView.addConstraints(
     NSLayoutConstraint.constraintsWithVisualFormat(
      "V:|-[headBttn]-|", options: nil, metrics: nil, views: viewsDict)) 

    return headerView 

} 
1

のthnxをinteresstedする必要がある場合は、ここでいくつかの研究が答えです。 Swiftにいくつかの変更が加えられているので、あなたの答えに影響します。私はスイフト3と4で同じことを行う方法の例を提供します:

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

    let title = UILabel() 
    title.translatesAutoresizingMaskIntoConstraints = false 
    title.text = "myTitle" 
    headerView.addSubview(title) 

    let button = UIButton(type: .system) 
    button.translatesAutoresizingMaskIntoConstraints = false 
    button.setTitle("myButton", for: .normal) 
    headerView.addSubview(button) 

    var headerViews = Dictionary<String, UIView>() 
    headerViews["title"] = title 
    headerViews["button"] = button 

    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|-10-[title]-[button]-15-|", options: [], metrics: nil, views: headerViews)) 
    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[title]-|", options: [], metrics: nil, views: headerViews)) 
    headerView.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-[button]-|", options: [], metrics: nil, views: headerViews)) 

    return headerView 
} 
関連する問題