在为视图标签

时间:2017-01-13 16:48:16

标签: swift xcode build

我正在构建一个简单的应用程序,我正在创建代码中的所有UI。该项目工作正常,直到我添加了一些标签的约束并添加一些东西到故事板(我发现故事板对线框图很有用)。

现在,当我尝试构建时,构建会在编译Swift文件期间冻结(Xcode仍会响应输入)。

之前(当项目建成时),我的班级看起来像这样:

class CurrentWeatherView: UIView {

    weak var tempLowLabel: UILabel! = UILabel()
    weak var tempLowIcon: UILabel! = UILabel()
    weak var tempHighLabel: UILabel! = UILabel()
    weak var tempHighIcon: UILabel! = UILabel()

    weak var precipChanceLabel: UILabel! = UILabel()
    weak var precipIconLabel: UILabel! = UILabel()
    weak var humidity: UILabel! = UILabel()
    weak var humidityIconLabel: UILabel! = UILabel()

    weak var icon: UILabel! = UILabel()
    weak var tempLabel: UILabel! = UILabel()

    weak var summaryLabel: UILabel! = UILabel()
    weak var summaryScroll: UIScrollView! = UIScrollView()
}

现在(当它不会构建时),它看起来像这样:

class CurrentWeatherView: UIView {

    weak var tempLowLabel: UILabel! = UILabel()
    weak var tempLowIcon: UILabel! = UILabel()
    weak var tempHighLabel: UILabel! = UILabel()
    weak var tempHighIcon: UILabel! = UILabel()

    weak var precipChanceLabel: UILabel! = UILabel()
    weak var precipIconLabel: UILabel! = UILabel()
    weak var humidityLabel: UILabel! = UILabel()
    weak var humidityIconLabel: UILabel! = UILabel()

    weak var icon: UILabel! = UILabel()
    weak var iconSummaryLabel: UILabel! = UILabel()
    weak var tempLabel: UILabel! = UILabel()

    weak var summaryLabel: UILabel! = UILabel()
    weak var summaryScroll: UIScrollView! = UIScrollView()

    func setUpLayout() {

        self.addSubview(tempLowLabel)
        self.addSubview(tempLowIcon)
        self.addSubview(tempHighLabel)
        self.addSubview(tempHighIcon)
        self.addSubview(precipChanceLabel)
        self.addSubview(precipIconLabel)
        self.addSubview(humidityLabel)
        self.addSubview(humidityIconLabel)
        self.addSubview(icon)
        self.addSubview(iconSummaryLabel)
        self.addSubview(tempLabel)
        self.addSubview(summaryLabel)
        self.addSubview(summaryScroll)

        tempLowLabel.translatesAutoResizingMaskToConstraints = false
        tempLowIcon.translatesAutoResizingMaskToConstraints = false
        tempHighLabel.translatesAutoResizingMaskToConstraints = false
        tempHighIcon.translatesAutoResizingMaskToConstraints = false
        precipChanceLabel.translatesAutoResizingMaskToConstraints = false
        precipIconLabel.translatesAutoResizingMaskToConstraints = false
        humidityLabel.translatesAutoResizingMaskToConstraints = false
        humidityIconLabel.translatesAutoResizingMaskToConstraints = false
        icon.translatesAutoResizingMaskToConstraints = false
        iconSummaryLabel.translatesAutoResizingMaskToConstraints = false
        tempLabel.translatesAutoResizingMaskToConstraints = false
        summaryLabel.translatesAutoResizingMaskToConstraints = false
        summaryScroll.translatesAutoResizingMaskToConstraints = false

        NSLayoutConstraint.activate([
                NSLayoutConstraint(item: tempHighIcon, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 12),
                NSLayoutConstraint(item: tempHighIcon, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 22),
                tempHighIcon.heightAnchor.constraint(equalToConstant: 22),
                tempHighIcon.widthAnchor.constraint(equalToConstant: 16),

                NSLayoutConstraint(item: tempLowIcon, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 12),
                NSLayoutConstraint(item: tempLowIcon, attribute: .top, relatedBy: .equal, toItem: tempHighIcon, attribute: .bottom, multiplier: 1.0, constant: 5),
                tempLowIcon.heightAnchor.constraint(equalToConstant: 22),
                tempLowIcon.widthAnchor.constraint(equalToConstant: 16),

                NSLayoutConstraint(item: tempHighLabel, attribute: .left, relatedBy: .equal, toItem: tempHighIcon, attribute: .right, multiplier: 1.0, constant: 8),
                NSLayoutConstraint(item: tempHighLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 22),
                tempHighIcon.heightAnchor.constraint(equalToConstant: 22),
                tempHighIcon.widthAnchor.constraint(equalToConstant: 42),

                NSLayoutConstraint(item: tempLowLabel, attribute: .left, relatedBy: .equal, toItem: tempLowIcon, attribute: .right, multiplier: 1.0, constant: 8),
                NSLayoutConstraint(item: tempLowLabel, attribute: .top, relatedBy: .equal, toItem: tempHighLabel, attribute: .bottom, multiplier: 1.0, constant: 5),
                tempHighIcon.heightAnchor.constraint(equalToConstant: 22),
                tempHighIcon.widthAnchor.constraint(equalToConstant: 42),

                NSLayoutConstraint(item: icon, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 22),
                icon.centerXAnchor.constraint(equalTo: self.centerXAnchor),
                icon.widthAnchor.constraint(equalToConstant: 85),
                icon.heightAnchor.constraint(equalToConstant: 85),

                iconSummaryLabel.centerXAnchor.constraint(equalTo: icon.centerXAnchor),
                iconSummaryLabel.centerYAnchor.constraint(equalTo: icon.centerYAnchor),
                iconSummaryLabel.heightAnchor.constraint(equalToConstant: 75),
                iconSummaryLabel.widthAnchor.constraint(equalToConstant: 75),

                NSLayoutConstraint(item: tempLabel, attribute: .top, relatedBy: .equal, toItem: icon, attribute: .bottom, multiplier: 1.0, constant: 8),
                tempLabel.centerXAnchor.constraint(equalTo: self.centerXAnchor),
                tempLabel.widthAnchor.constraint(equalToConstant: 85),
                tempLabel.heightAnchor.constraint(equalToConstant: 25),

                NSLayoutConstraint(item: summaryScroll, attribute: .left, relatedBy: .equal, toItem: self, attribute: .left, multiplier: 1.0, constant: 12),
                NSLayoutConstraint(item: summaryScroll, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 12),
                NSLayoutConstraint(item: summaryScroll, attribute: .bottom, relatedBy: .equal, toItem: self, attribute: .bottom, multiplier: 1.0, constant: 8),
                summaryScroll.heightAnchor.constraint(equalToConstant: 21),
                summaryScroll.centerXAnchor.constraint(equalTo: self.centerXAnchor),

                NSLayoutConstraint(item: precipChanceLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 12),
                NSLayoutConstraint(item: precipChanceLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 22),
                precipChanceLabel.heightAnchor.constraint(equalToConstant: 22),
                precipChanceLabel.widthAnchor.constraint(equalToConstant: 42),

                NSLayoutConstraint(item: humidityLabel, attribute: .right, relatedBy: .equal, toItem: self, attribute: .right, multiplier: 1.0, constant: 12),
                NSLayoutConstraint(item: humidityLabel, attribute: .top, relatedBy: .equal, toItem: precipChanceLabel, attribute: .bottom, multiplier: 1.0, constant: 5),
                humidityLabel.heightAnchor.constraint(equalToConstant: 22),
                humidityLabel.widthAnchor.constraint(equalToConstant: 42),

                NSLayoutConstraint(item: precipIconLabel, attribute: .right, relatedBy: .equal, toItem: precipChanceLabel, attribute: .left, multiplier: 1.0, constant: 8),
                NSLayoutConstraint(item: precipIconLabel, attribute: .top, relatedBy: .equal, toItem: self, attribute: .top, multiplier: 1.0, constant: 22),
                precipIconLabel.heightAnchor.constraint(equalToConstant: 22),
                precipIconLabel.widthAnchor.constraint(equalToConstant: 16),

                NSLayoutConstraint(item: humidityIconLabel, attribute: .right, relatedBy: .equal, toItem: humidityLabel, attribute: .left, multiplier: 1.0, constant: 8),
                NSLayoutConstraint(item: humidityIconLabel, attribute: .top, relatedBy: .equal, toItem: precipIconLabel, attribute: .bottom, multiplier: 1.0, constant: 5),
                humidityIconLabel.heightAnchor.constraint(equalToConstant: 22),
                humidityIconLabel.widthAnchor.constraint(equalToConstant: 16),
            ])
    }
}

对于Xcode来说,所有这些限制是否都有很多?

此外,索引编制还没有完成。

我试过清理项目,清理构建文件夹,删除派生数据,重新启动我的机器,重新启动Xcode,更新Xcode和检查代码优化级别,但没有一个帮助。

1 个答案:

答案 0 :(得分:0)

我能够通过更新到Xcode版本8.3 beta来解决这个问题。 Xcode 8.3刚刚发布,所以也可以使用。