以编程方式添加标签,与Storyboard中的标签对齐
期望的观点:
在Storyboard中,我有两个标签,其中包含我在Autolayout中创建的蓝色背景。 他们的立场永远不会改变。 接下来,我想在蓝色背景标签下面的cellForRowAtIndexPath
中的代码中添加1到10个标签。
我正在努力将代码中添加的标签(棕色背景)与在Autolayout(蓝色背景)中创建的标签对齐。
以下是我失败的尝试:
失败的两种方法:
-
在
cellForRowAtIndexPath
获取“B AutoLayout静态标签”的框架,并使用X位置作为动态标签。 不工作。 -
添加约束也不起作用 – 也许我没有正确添加约束。
这是代码:
class TableViewController: UITableViewController { var cellHeight = [Int: CGFloat]() override func viewDidLoad() { super.viewDidLoad() tableView.estimatedRowHeight = 85.0 self.tableView.rowHeight = UITableViewAutomaticDimension } override func numberOfSectionsInTableView(tableView: UITableView) -> Int { return 1 } override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { return 4 } override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! CustomCell let xLocation = cell.labelBStatic.frame.origin.x var yLocation = cell.labelBStatic.frame.origin.y let height = cell.labelBStatic.frame.size.height var startYLocation = yLocation + height + 20 var i = 0 if indexPath.row % 2 == 0 { i = 5 } else { i = 7 } while i =50)]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary) labelView.addConstraints(view1_constraint_H) //position constraints let view_constraint_H:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("H:|-15-[view1]", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: viewsDictionary) let view_constraint_V:NSArray = NSLayoutConstraint.constraintsWithVisualFormat("V:|-\(verticalSpacing)-[view1]", options: NSLayoutFormatOptions.AlignAllLeading, metrics: nil, views: viewsDictionary) view.addConstraints(view_constraint_H as! [NSLayoutConstraint]) view.addConstraints(view_constraint_V as! [NSLayoutConstraint]) } override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat { if let height = cellHeight[indexPath.row] { return height } return 0 }
下面是Storyboard
设置(两个标签都水平居中):
问题:如何让我在cellForRowAtIndexPath
创建的动态标签与我在Storyboard
中创建的静态标签保持一致,以匹配我在顶部的所需视图?
出于演示目的,我已经以编程方式添加了一个标签,您可以添加所需的标签,只需正确添加约束,还需要将bottomSpace约束添加到单元格内的最后一个标签,以便您的单元格将根据需要自动resize。标签高度。
按照我所做的步骤来实现您的目标:
-
访问cellForRowAtIndexPath中的B AutoLayout静态标签:如果您已经创建了子类UITableViewCell并创建了sockets,则使用标签或sockets。
let labelBAutolayoutStaticLabel = cell?.viewWithTag(20)
-
以编程方式创建标签,如下所示,并将translatesAutoresizingMaskIntoConstraints设置为false,
let labelDynamicLabel = UILabel() labelDynamicLabel.backgroundColor = UIColor.orangeColor() labelDynamicLabel.text = "A Dynamic Label" labelDynamicLabel.translatesAutoresizingMaskIntoConstraints = false cell?.contentView.addSubview(labelDynamicLabel)
-
您需要创建两个约束,一个用于TopSpace,第二个是LeadingSpace,如下所示,
let leadingSpaceConstraint: NSLayoutConstraint = NSLayoutConstraint(item: labelDynamicLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: labelBAutolayoutStaticLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0); let topSpaceConstraint: NSLayoutConstraint = NSLayoutConstraint(item: labelDynamicLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: labelBAutolayoutStaticLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10); //Constant is the spacing between
-
添加约束到您的单元格的contentView,如下所示,
cell?.contentView.addConstraint(leadingSpaceConstraint) cell?.contentView.addConstraint(topSpaceConstraint)
而已。
这是结果,
以下是cellForRowAtIndexPath的完整代码:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCellWithIdentifier("cell") let labelBAutolayoutStaticLabel = cell?.viewWithTag(20) let labelDynamicLabel = UILabel() labelDynamicLabel.backgroundColor = UIColor.orangeColor() labelDynamicLabel.text = "A Dynamic Label" labelDynamicLabel.translatesAutoresizingMaskIntoConstraints = false cell?.contentView.addSubview(labelDynamicLabel) let leadingSpaceConstraint: NSLayoutConstraint = NSLayoutConstraint(item: labelDynamicLabel, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: labelBAutolayoutStaticLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0); let topSpaceConstraint: NSLayoutConstraint = NSLayoutConstraint(item: labelDynamicLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: labelBAutolayoutStaticLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 10); cell?.contentView.addConstraint(leadingSpaceConstraint) cell?.contentView.addConstraint(topSpaceConstraint) return cell! }
编辑/更新:
如果必须将bottomSpace约束设置为最后一个标签(位于单元格底部的标签),则有两种方法
-
使用NSLayoutConstraint如下:
let bottomSpaceConstraint: NSLayoutConstraint = NSLayoutConstraint(item: labelDynamicLabel, attribute: NSLayoutAttribute.BottomMargin, relatedBy: NSLayoutRelation.Equal, toItem: cell.contentView, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: -8) cell.contentView.addConstraint(bottomSpaceConstraint)
-
使用如下的视觉格式语言,
let views = ["cell": cell, "labelDynamicLabel": labelDynamicLabel, "labelBAutolayoutStaticLabel": labelBAutolayoutStaticLabel] let verticalConstraints = NSLayoutConstraint.constraintsWithVisualFormat("V:[labelBAutolayoutStaticLabel]-[labelDynamicLabel]-|", options: [], metrics: nil, views: views) cell.contentView.addConstraints(verticalConstraints)
如果使用VFL设置约束,请确保删除topSpaceConstraint
//cell.contentView.addConstraint(topSpaceConstraint)
这个“V:[labelBAutolayoutStaticLabel] – [labelDynamicLabel] – |” 字符串的意思是,
- labelDynamicLabel应该将TopSpace标记为带有standardSpacing(8pts)的labelButolayoutStaticLabel,而labelDynamicLabel应该具有使用standardSpacing的SuperView的底部空间。 (’ – |’表示superView的标准空间)