【问题标题】:Issue with constraints set programmatically以编程方式设置约束的问题
【发布时间】:2017-07-14 15:03:32
【问题描述】:

我整天都在为此苦苦挣扎 - 我需要在导航栏中添加一个视图作为 rightBarButtonItems,其中包含一个 UILabel 和一个 UIImageView。因此,我需要以编程方式创建视图,设置约束以编程方式将视图添加为 rightBarButtonItems。

我想要实现的是:

.

.

这就是我得到的:

好像不管我做什么,我都不能移动向下箭头..它需要在标签的右侧,并与centerY对齐。

这是我的代码:

    //Elements
    let containerView = UIView()
    containerView.frame = CGRect(x: 0, y: 0, width: 90, height: 30)
    containerView.backgroundColor = UIColor.blueColor()

    let codedLabel:UILabel = UILabel()
    codedLabel.frame = CGRect(x: 0, y: 0, width: 80, height: 30)
    codedLabel.textAlignment = .Center
    codedLabel.text = "FILTRER"
    codedLabel.numberOfLines = 1
    codedLabel.textColor = UIColor.redColor()
    codedLabel.font = UIFont(name: Constants.ubuntuBold, size: 18.0)!
    codedLabel.backgroundColor = UIColor.lightGrayColor()
    codedLabel.sizeToFit()

    let codedImageView: UIImageView = UIImageView()
    codedImageView.frame = CGRect(x: 0, y: 0, width: 10, height: 5.7)
    codedImageView.image = UIImage(named: "dragToRefreshArrow")
    codedImageView.backgroundColor = UIColor.cyanColor()

    containerView.addSubview(codedLabel)
    containerView.addSubview(codedImageView)

    containerView.sizeToFit()

    //Constraints
   containerView.translatesAutoresizingMaskIntoConstraints = false

    //Label
    NSLayoutConstraint(item: codedLabel, attribute: .Top, relatedBy: .Equal, toItem: containerView, attribute: .Top, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: codedLabel, attribute: .Bottom, relatedBy: .Equal, toItem: containerView, attribute: .Bottom, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: codedLabel, attribute: .Leading, relatedBy: .Equal, toItem: containerView, attribute: .Leading, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: codedLabel, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true

    //ImageView
    NSLayoutConstraint(item: codedImageView, attribute: .Leading, relatedBy: .Equal, toItem: codedLabel, attribute: .Leading, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: codedImageView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true
    NSLayoutConstraint(item: codedImageView, attribute: .CenterY, relatedBy: .Equal, toItem: codedLabel, attribute: .Top, multiplier: 1, constant: 0).active = true

    let item = UIBarButtonItem()
    item.customView = containerView

    var negativeSpace:UIBarButtonItem = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FixedSpace, target: nil, action: nil)
    negativeSpace.width = -10.0

    self.navigationItem.rightBarButtonItems = [negativeSpace, item]

有人知道我做错了什么吗? :-)

【问题讨论】:

  • 是的,使用编程自动布局的所有视图必须将translatesAutoresizingMaskIntoConstraints设置为false
  • @BallpointBen- 这可能是问题所在 :) 现在我至少可以移动箭头了!谢啦!当我让它正常工作时,我会发布最终解决方案:))

标签: swift constraints swift2.3


【解决方案1】:

为了使标签(codedLabel)右侧的箭头(codedImageView),对齐CenterY和容器内部(ContainerView),您需要以下约束:

  • codedImageView.leading = codedLabel.trailing => 这会将 codedImageView 向右移动 codedLabel
  • codedImageView.centerY = codedLabel.centerY => 这将垂直居中
  • codedImageView.trailing = containerView.trailing => 这会将它设置在容器视图的末尾和内部。

这产生了以下约束:

NSLayoutConstraint(item: codedImageView, attribute: .Leading, relatedBy: .Equal, toItem: codedLabel, attribute: .Trailing, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedImageView, attribute: .Trailing, relatedBy: .Equal, toItem: containerView, attribute: .Trailing, multiplier: 1, constant: 0).active = true
NSLayoutConstraint(item: codedImageView, attribute: .CenterY, relatedBy: .Equal, toItem: codedLabel, attribute: .CenterY, multiplier: 1, constant: 0).active = true

看看第一个和第三个约束有何不同?在您的示例中,您将其附加到 codedLabel 的左上角而不是右中角。

【讨论】:

  • 谢谢托马斯!我刚刚尝试了您的代码,但箭头仍然卡在左上角..好像我根本无法移动它..我不确定这是因为容器视图周围没有任何约束,因为它是在导航栏中.. 可能是?
【解决方案2】:

您需要将约束添加到视图。类似的东西:

let myConstraint = NSLayoutConstraint(....)
myConstraint.active = ture
self.containerView.addConstraints(myConstraint)

【讨论】:

  • 抱歉,我不确定这应该有什么帮助:)?你能再解释一下吗?这似乎是我已经添加的第一个约束?
  • @NicolaiHarbo 老兄,你没有给任何东西添加约束。我想说的是将约束设置为 var,然后将该 var 添加到视图中。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 2013-06-19
  • 1970-01-01
  • 2020-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多