正确标题方面适合图像

时间:2017-02-21 20:06:16

标签: swift image imageview position

我正在寻找在图像视图设置为纵横比的任何图像的顶部和底部添加标题的正确方法。看起来很简单,但文本框似乎并没有在大部分时间移动,而是在其他时间以中心结束。这是我为顶部标题尝试的公式的示例。

mainTextBox.center.y = mainImageView.center.y + (mainImageView.image!.size.height / 2) - (mainTextBox.frame.size.height / 2)

在Vandan Patel的帮助下更新:

我也尝试手动设置约束手动购买收到错误

    let titleLabelWidth = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Width, multiplier: 0.9, constant: 0)
    let titleLabelCenterX = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)

    let titleLabelCenterY = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
    let titleLabelHeight = NSLayoutConstraint(item: mainTextBox, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 0.9, constant: 0)
    mainTextBox.addConstraints([titleLabelWidth, titleLabelCenterX, titleLabelCenterY, titleLabelHeight])

*由于未捕获的异常终止应用' NSInvalidArgumentException',原因:' * + [NSLayoutConstraint constraintWithItem:attribute:relatedBy:toItem:attribute:mult iplier: constant:]:乘数0或零第二项与第一个属性的位置一起创建一个等于常量的位置的非法约束。必须成对指定位置属性'

1 个答案:

答案 0 :(得分:2)

你在寻找这样的东西吗?enter image description here

//title-Label Constraints

let titleLabelWidth = NSLayoutConstraint(item: titleLabel, attribute: .width, relatedBy: .equal, toItem: blurEffectView, attribute: .width, multiplier: 0.9, constant: 0)
let titleLabelCenterX = NSLayoutConstraint(item: titleLabel, attribute: .centerX, relatedBy: .equal, toItem: blurEffectView, attribute: .centerX, multiplier: 1, constant: 0)
let titleLabelCenterY = NSLayoutConstraint(item: titleLabel, attribute: .centerY, relatedBy: .equal, toItem: blurEffectView, attribute: .centerY, multiplier: 1, constant: 0)
let titleLabelHeight = NSLayoutConstraint(item: titleLabel, attribute: .height, relatedBy: .equal, toItem: blurEffectView, attribute: .height, multiplier: 0.9, constant: 0)

self.addConstraints([titleLabelWidth, titleLabelCenterX, titleLabelCenterY, titleLabelHeight])