使用可视化格式语言创建三列(Swift)

时间:2016-01-27 21:17:34

标签: swift nslayoutconstraint visual-format-language

我正在尝试使用VFL(可视化格式语言)

创建具有以下布局的视图
-------------------------------------------------
| [LeftView]      [CenterView]       [RightView]|
-------------------------------------------------

但是它出现了这样的

-------------------------------------------------
| [LeftView]                         [RightView]|
-------------------------------------------------

我认为问题在于这一行

H:|-10-[cancel(70)]-[title]-[save(70)]-10-|

这是代码的其余部分 我不知道我做错了什么。这是我的代码:

    let headerView = UIView(frame: CGRectMake(0,0,mainFrame.width, headerHeight))
    headerView.backgroundColor = BG_COLOR
    self.addSubview(headerView)

    let cancelButton = UIButton(type: .Custom)
    cancelButton.translatesAutoresizingMaskIntoConstraints = false
    cancelButton.setTitle("Cancel", forState: .Normal)
    cancelButton.addTarget(self, action: "cancelButtonClick:", forControlEvents: .TouchUpInside)
    headerView.addSubview(cancelButton)

    let titleLabel = UILabel()
    titleLabel.text = "Bedroom"
    titleLabel.textColor = UIColor.whiteColor()
    headerView.addSubview(titleLabel)

    let saveButton = UIButton(type: .Custom)
    saveButton.titleLabel?.textAlignment = NSTextAlignment.Right
    saveButton.translatesAutoresizingMaskIntoConstraints = false
    saveButton.setTitle("Save", forState: .Normal)
    saveButton.addTarget(self, action: "saveButtonClick:", forControlEvents: .TouchUpInside)
    headerView.addSubview(saveButton)

    let views = ["title":titleLabel, "cancel":cancelButton,"save":saveButton,"header":headerView]

    headerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|[save]|",
            options:[.AlignAllCenterY],
            metrics:nil,
            views:views)
    )

    headerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|[cancel]|",
            options:[.AlignAllCenterY],
            metrics:nil,
            views:views)
    )

    headerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "V:|[title]|",
            options:[.AlignAllCenterY],
            metrics:nil,
            views:views)
    )

    headerView.addConstraints(
        NSLayoutConstraint.constraintsWithVisualFormat(
            "H:|-10-[cancel(70)]-[title]-[save(70)]-10-|",
            options:[.AlignAllCenterY],
            metrics:nil,
            views:views)
    )

1 个答案:

答案 0 :(得分:0)

您的代码看起来很好。根据您描述的现象,中心视图不可见。这可能有几个原因。

  • 没有内容。
  • 您在某处设置了hidden属性。
  • 您已将alpha属性设置为零。
  • 您正在覆盖layoutSubviews
  • 中的布局
  • 标签文字与背景颜色相同。

除了许多其他原因......无论如何,它这行VFL。