ios - 如何在 "viewForHeaderInSection"中为 UILabel View 设置自动布局约束

标签 ios swift uitableview autolayout constraints

 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 固定在标题的右侧,看起来很糟糕。我需要留一点空间。

最佳答案

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

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
}

关于ios - 如何在 "viewForHeaderInSection"中为 UILabel View 设置自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39564174/

相关文章:

ios - 递归和 objective-c

ios - ABRecordGetRecordID 返回 -1

ios - NumberFormatter 错误结果为 0.9972

ios - 查找周末发生的所有核心数据条目(使用 NSDate)

ios - UITableView:在 iOS 10 中可能在点击/更新和删除行后禁用滚动和滑动手势

android - 如何修复 "Invariant Objects are not valid as a react Child"

ios - Xcode 9 Swift 4 标签页

ios - UITableView 删除行减少表格行高

ios - 为什么表格中的第一节标题未显示?

swift - queryOrdered 并不总是以正确的顺序返回 tableViewCell 上的数据