使用Swift 3 Xcode 8对齐多个标签

时间:2016-10-07 23:28:01

标签: swift xcode uilabel

我想将三个标签作为一组对齐,以水平居中。其中两个标签的大小可变。我完全提到了以下帖子,但我的应用程序崩溃了以下消息:

发表: Center align Labels and images inside UIView programatically with Swift

错误消息:

  

添加到视图时,约束的项必须是后代   该视图(或视图本身)。如果约束,这将崩溃   需要在组装视图层次结构之前解决。

一些事情: 我已经在Autolayout中对这三个标签有限制。对于" InLabel"我从顶部邻居的边距,宽度和间距有前导空间。对于其他两个标签(durationLable& timeUnitLabel),我在所有三个标签之间留有空格,并且与顶部邻居之间有间距。

UIView在类ViewController的开头部分初始化为indurtimeView = UIView()

我在" viewDidLoad"中调用函数indurtimeLabelcenter()在ViewController类中调用super.viewDidLoad()之后的函数。

这是我的函数indurtimeLabelcenter()的代码:

func indurtimeLabelcenter() {
    //Align "In", "Duration Lable" and "Time Unit Label" to center
    self.view.addSubview(indurtimeView)
    indurtimeView.translatesAutoresizingMaskIntoConstraints = false
    indurtimeView.clipsToBounds = true
    indurtimeView.addSubview(InLabel)

    //Constraints
    var viewsDict = Dictionary <String, UIView>()
    viewsDict["InLabel"] = InLabel
    viewsDict["durationLabel"] = durationLabel
    viewsDict["timeunitLabel"] = timeUnitLabel

    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[InLabel]|", options: [], metrics: nil, views: viewsDict))
    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[durationLabel]|", options: [], metrics: nil, views: viewsDict))
    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[timeunitLabell]|", options: [], metrics: nil, views: viewsDict))
    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-[InLabel]-5-[durationLabel]-5-[timeunitLabel]-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: viewsDict))
    //costView!.backgroundColor = UIColor.redColor()

    // center costView inside self
    let centerXCons = NSLayoutConstraint(item: indurtimeView, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0);
    let centerYCons = NSLayoutConstraint(item: indurtimeView, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0);
    self.view.addConstraints([centerXCons, centerYCons])
}

第一次在Swift中编写应用程序,所以请忍受我的无知。

2 个答案:

答案 0 :(得分:0)

在您的代码中,您似乎可能在以下行中出错:

var viewsDict = Dictionary <String, UIView>()

我相信您的意思是使您的词典值类型为UILabel。但是,您将其作为UIView类型。唯一的变化是:

var viewsDict = Dictionary <String, UILabel>()

在任何一种情况下,要回答您的问题,您可以通过执行以下操作将多个标签居中对齐:

var viewsDict = Dictionary <String, UILabel>()
viewsDict["InLabel"] = InLabel
viewsDict["durationLabel"] = durationLabel
viewsDict["timeunitLabel"] = timeUnitLabel

for label in viewsDict.values {
    label.center.x = view.bounds.width/2
}

循环遍历字典值,将每个UILabel的中心x值指定给屏幕中心。

<强>更新 手动计算,但如果你不能让你的约束工作,总是可以这样做。

let labelOneXOrigin = screenWidth-10-labelOne.frame.width-labelTwo.frame.width-labelThree.frame.width
let labelTwoXOrigin = labelOne.frame.origin.x+labelOne.frame.width+5
let labelThreeXOrigin = labelTwo.frame.origin.x+labelTwo.frame.width+5

答案 1 :(得分:0)

确定。终于......我明白了。有几个问题。首先,我只为一个标签添加了子视图。必须为所有三个标签添加它(需要在中心对齐)。其次,由于我想将它们水平对齐,我不需要提供“centerYcons”。第三,其中一个标签有左边距恒定空间的约束。它需要被删除,因为它不能满足对中和它的限制以及左边距的恒定空间。我在下面提供了工作代码:

func indurtimeLabelcenter() {
    //Align "In", "Duration Lable" and "Time Unit Label" to center
    self.view.addSubview(indurtimeView)
    indurtimeView.translatesAutoresizingMaskIntoConstraints = false

    indurtimeView.addSubview(InLabel)
    indurtimeView.addSubview(durationLabel)
    indurtimeView.addSubview(timeUnitLabel)

    //Constraints
    var viewsDict = Dictionary <String, UIView>()
    viewsDict["InLabel"] = InLabel
    viewsDict["durationLabel"] = durationLabel
    viewsDict["timeunitLabel"] = timeUnitLabel

    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[InLabel]|", options: [], metrics: nil, views: viewsDict))
    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[durationLabel]|", options: [], metrics: nil, views: viewsDict))
    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "V:|[timeunitLabel]|", options: [], metrics: nil, views: viewsDict))
    indurtimeView.addConstraints(
        NSLayoutConstraint.constraints(
            withVisualFormat: "H:|-[InLabel]-5-[durationLabel]-5-[timeunitLabel]-|", options: NSLayoutFormatOptions.alignAllCenterY, metrics: nil, views: viewsDict))

    // center costView inside self
    let centerXCons = NSLayoutConstraint(item: indurtimeView, attribute: .centerX, relatedBy: .equal, toItem: self.view, attribute: .centerX, multiplier: 1, constant: 0);
    //let centerYCons = NSLayoutConstraint(item: indurtimeView, attribute: .centerY, relatedBy: .equal, toItem: self.view, attribute: .centerY, multiplier: 1, constant: 0);
    self.view.addConstraints([centerXCons])
}