Swift 向视图标签添加约束后,Xcode在生成中冻结
我正在构建一个简单的应用程序,并在代码中创建所有UI。该项目运行良好,直到我添加了一些标签的约束,并向情节提要中添加了一些内容(我发现情节提要对线框非常有用) 现在,当我尝试构建时,构建在编译Swift文件期间冻结(Xcode仍然响应输入) 在此之前(项目将要构建的时候),我的类是这样的:Swift 向视图标签添加约束后,Xcode在生成中冻结,swift,xcode,build,Swift,Xcode,Build,我正在构建一个简单的应用程序,并在代码中创建所有UI。该项目运行良好,直到我添加了一些标签的约束,并向情节提要中添加了一些内容(我发现情节提要对线框非常有用) 现在,当我尝试构建时,构建在编译Swift文件期间冻结(Xcode仍然响应输入) 在此之前(项目将要构建的时候),我的类是这样的: class CurrentWeatherView: UIView { weak var tempLowLabel: UILabel! = UILabel() weak var tempLow
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),
])
}
}
现在(当它无法构建时),它看起来是这样的:
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并检查代码优化级别,但没有任何帮助。我能够通过更新到Xcode 8.3 beta版来解决此问题。Xcode 8.3刚刚发布,所以应该也可以使用。(1)当我看到“索引…”时,我学会了让Xcode单独使用。拿起一杯你最喜欢的饮料,在网上蹦蹦跳跳,小睡一会儿,随便什么。不要打断它。:-)(2) 我很好奇你为什么把你所有的UIKit变量都设置为弱。虽然这是IBOutlet的默认值,但我从来没有这样声明过我的方法-这并不意味着我的方法更正确,这只是一个主要的区别。@dfd我选择弱的原因有两个:1)IBOutlet是这样声明的,所以我认为这是你应该使用的方法。2) 我想这会有助于提高记忆力。你这样做有什么具体的原因吗?还有,什么是索引,我为什么不去处理它呢?这里的约束数量没有什么值得注意的。Xcode可以而且应该能够处理这一点(以及更多),我以我一直看到的方式编写代码——尽管我很少使用Obj-C,即使在那里也是如此。我没有比这更好的理由了。您声称Xcode在尝试构建时“冻结”——我不怀疑这一点,因此我正在研究可能存在的问题。至于索引,我记得在某处(可能在这里)看到一条评论,大意是“看到索引时,让Xcode做任何事情”。我已经让它发生在我身上了。。。我不明白为什么我无法构建,并尝试了从重新启动到拔下设备的所有操作,结果发现一切都不起作用。@dfd抱歉。我解释错了。身体冻结了。Xcode没有。请注意我对解释的编辑。>构建冻结(Xcode仍然响应输入)