6

I have a tableview and I want to have a UIStackView in every section header. Now, I want to give a little padding to this stackView: in particular, I want the stackView to take the entire space except for 2px for every side. Is this possible? This is what I tried:

override func tableView(tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {

        let sNumber = UILabel()
        sNumber.backgroundColor = UIColor.yellowColor()
        sNumber.text = String(section)+". "

        let lbl = UILabel()
        lbl.backgroundColor = UIColor.cyanColor()
        lbl.text = (detailItem![section]["canary"] as! String)

        let lbl2 = UILabel()
        lbl2.backgroundColor = UIColor.greenColor()
        lbl2.text = (detailItem![section]["tbd"] as! String)

        let stackView   = UIStackView()
        stackView.axis  = UILayoutConstraintAxis.Horizontal
        stackView.distribution  = UIStackViewDistribution.FillProportionally
        stackView.alignment = UIStackViewAlignment.Center
        stackView.spacing = 10

        let margins = stackView.layoutMarginsGuide
        stackView.leadingAnchor.constraintEqualToAnchor(margins.leadingAnchor, constant: 2).active = true
        stackView.trailingAnchor.constraintEqualToAnchor(margins.trailingAnchor, constant: 2).active = true
        stackView.topAnchor.constraintEqualToAnchor(margins.topAnchor, constant: 2).active = true
        stackView.bottomAnchor.constraintEqualToAnchor(margins.bottomAnchor, constant: 2).active = true

        stackView.addArrangedSubview(sNumber)
        stackView.addArrangedSubview(lbl)
        stackView.addArrangedSubview(lbl2)

        return stackView
    }

Any help is appreciated.

0 Answers0