【问题标题】:How can I set an auto layout constraint in "viewForHeaderInSection" for a UILabel view如何在“viewForHeaderInSection”中为 UILabel 视图设置自动布局约束
【发布时间】:2017-01-26 14:41:35
【问题描述】:
 func tableView(tableView:UITableView, viewForHeaderInSection section:Int) -> UIView?{
        let newlabel = UILabel()
        //206-250

        newlabel.backgroundColor = UIColor(red: (135/255), green:(206/255), blue: (250/255), alpha: 1)
        newlabel.textColor = UIColor(white: 1, alpha: 1)
        newlabel.textAlignment = .Right
        newlabel.font = newlabel.font.fontWithSize(18)
        newlabel.adjustsFontSizeToFitWidth = true
        let horizontalcontraint = NSLayoutConstraint(item: newlabel, attribute: .Trailing, relatedBy: .Equal, toItem: view, attribute: .Trailing, multiplier: 1, constant: -20)
        NSLayoutConstraint.activateConstraints([horizontalcontraint])

        newlabel.constraints
        newlabel.text = keys[section]+" - "
        return newlabel
    }

我不确定如何在约束的toItem: 部分引用节标题。任何意见,将不胜感激。 UILabel 被固定在标题的右侧,看起来很糟糕。我需要一点间距。

【问题讨论】:

  • 将值 -20 更改为 20 并尝试,但我认为它不起作用,因为您只是使用标签。根据我的说法,您应该使用 UIView,然后将所有约束之王从标签添加到视图并将 UIView 作为标题返回。

标签: ios swift uitableview autolayout constraints


【解决方案1】:

我已经修改了您的代码,就在这里。尝试一次。

func tableView(tableView:UITableView, viewForHeaderInSection section:Int) -> UIView?{
    let headerView = UIView()
    headerView.backgroundColor = UIColor.clearColor()
    let newlabel = UILabel()
    //206-250

    newlabel.backgroundColor = UIColor(red: (135/255), green:(206/255), blue: (250/255), alpha: 1)
    newlabel.textColor = UIColor(white: 1, alpha: 1)
    newlabel.textAlignment = .Right
    newlabel.font = newlabel.font.fontWithSize(18)
    newlabel.adjustsFontSizeToFitWidth = true

    newlabel.constraints
    newlabel.text = keys[section]+" - "

    headerView.addSubview(newlabel)
    newlabel.translatesAutoresizingMaskIntoConstraints = false
    headerView.addConstraint(NSLayoutConstraint(item: newlabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: headerView, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: 0))
    headerView.addConstraint(NSLayoutConstraint(item: newlabel, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: headerView, attribute: NSLayoutAttribute.Trailing, multiplier: 1.0, constant: 20.0))
    headerView.addConstraint(NSLayoutConstraint(item: newlabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: headerView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0))
    headerView.addConstraint(NSLayoutConstraint(item: newlabel, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: headerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0))

    return headerView
}

【讨论】:

  • translatesAutoresizingMaskIntoConstraints = false 适合我。一定为此浪费了很多时间
猜你喜欢
  • 1970-01-01
  • 2019-05-13
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 1970-01-01
  • 2017-06-14
相关资源
最近更新 更多