6

我有一个表格视图,我想UIStackView在每个部分标题中都有一个。现在,我想给这个 stackView 加一点填充:特别是,我希望 stackView 占据除了每边 2px 之外的整个空间。这可能吗?这是我尝试过的:

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
    }

任何帮助表示赞赏。

4

0 回答 0