我正在尝试使用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)
)
答案 0 :(得分:0)
您的代码看起来很好。根据您描述的现象,中心视图不可见。这可能有几个原因。
hidden
属性。 alpha
属性设置为零。layoutSubviews
。除了许多其他原因......无论如何,它不这行VFL。