UIStackView - 删除样式标签子导致崩溃

时间:2016-04-04 06:24:37

标签: ios swift uilabel uistackview

我遇到崩溃,有时当我尝试删除arrangedSubviews中的所有UIStackView时。如果我只使用UILabel没有样式,那么它可以正常工作。

我有这个功能来删除孩子:

extension UIStackView {
    func removeAllArrangedSubviews() {
        for child in self.arrangedSubviews {
            self.removeArrangedSubview(child)
            child.removeFromSuperview()
        }
    }
}

我像这样设置了UILabel:

public class func setToLabel(label:UILabel) {
    label.setFontRegular(10)
    label.setFontColor(BaseStyleColors.c4)
}

我有这个来设置字体:

public class FontHelpers {
    public class func systemFontWithMonospacedNumbers(currentFont:UIFont) -> UIFont {
        let features = [
            [
                UIFontFeatureTypeIdentifierKey: kNumberSpacingType,
                UIFontFeatureSelectorIdentifierKey: kMonospacedNumbersSelector
            ]
        ]

        let fontDescriptor = currentFont.fontDescriptor().fontDescriptorByAddingAttributes(
            [UIFontDescriptorFeatureSettingsAttribute: features]
        )

        return UIFont(descriptor: fontDescriptor, size: currentFont.pointSize)
    }

    public static var bodyStyleFont = UIFont.preferredFontForTextStyle(UIFontTextStyleBody)
    public static var headlineStyleFont = UIFont.preferredFontForTextStyle(UIFontTextStyleHeadline)

    public class func useBodyFontWithSize(size:CGFloat) -> UIFont {
        return UIFont(descriptor: self.bodyStyleFont.fontDescriptor(), size: size)
    }

    public class func useHeadlineFontWithSize(size:CGFloat) -> UIFont {
        return UIFont(descriptor: self.headlineStyleFont.fontDescriptor(), size: size)
    }

    public class func useSystemFontMediumWithSize(size:CGFloat) -> UIFont {
        return UIFont(name: ".SFUIText-Medium", size: size) ?? self.useBodyFontWithSize(size)
    }

    public class func useSystemFontSemiBoldWithSize(size:CGFloat) -> UIFont {
        return UIFont(name: ".SFUIText-SemiBold", size: size) ?? self.useBodyFontWithSize(size)
    }

}

标签扩展

extension UILabel {
    func setFontColor(color:UIColor) {
        self.textColor = color
    }

    func setFontDisplayMedium(size:CGFloat) {
        self.font = FontHelpers.systemFontWithMonospacedNumbers(FontHelpers.useSystemFontMediumWithSize(size))
    }

    func setFontMedium(size:CGFloat) {
        self.font = FontHelpers.useSystemFontMediumWithSize(size)
    }

    func setFontRegular(size:CGFloat) {
        self.font = FontHelpers.useBodyFontWithSize(size)
    }

    func setFontSemiBold(size:CGFloat) {
        self.font = FontHelpers.useSystemFontSemiBoldWithSize(size)
    }
}

在控制台中我收到此错误:

Auto layout internal error.  Cannot find an outgoing row head for incoming head during optimization of variable with near-zero coefficient, which should never happen.
2016-04-04 08:09:59.877 MyApp[49788:4979069] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '{objective 0x7f9fe3fc9350: <250:289.293> + <250:-3.58537>*0x7f9fe141ccf0:UISV-canvas-connection.marker{id: 1121} + <250:2.58537>*0x7f9fe141d080:UISV-alignment.marker{id: 1123} + <998:-0.999999, 250:2.58537>*0x7f9fe1439130:UISV-alignment.marker{id: 1094} + <998:0.999999, 250:-2.58537>*0x7f9fe1439250:UISV-alignment.marker{id: 1095} + <750:1, 250:1>*0x7f9fe14395e0.negError{id: 1099} + <750:1, 250:1>*0x7f9fe1439750.negError{id: 1126} + <750:1, 250:1>*0x7f9fe1439a10.negError{id: 1103} + <750:1, 250:1>*0x7f9fe1439a70.negError{id: 1105} + <998:1, 250:-0.585366>*0x7f9fe1439e10:UISV-canvas-connection.marker{id: 1106} + <998:-1, 250:0.585366>*0x7f9fe143a010:UISV-canvas-connection.marker{id: 1113} + <998:-1, 250:0.585366>*0x7f9fe143a6f0:UISV-spacing.marker{id: 1115} + <999:1, 998:1, 250:-1.58537>*0x7f9fe143a860:UISV-fill-proportionally.negError{id: 1117} + <999:1, 998:-1, 250:1.58537>*0x7f9fe143a860:UISV-fill-proportionally.posErrorMarker{id: 1116} + <998:2>*0x7f9fe143abd0:UISV-fill-proportionally.negError{id: 1119} + <250:3.58537>*0x7f9fe143ae70:UISV-canvas-connection.marker{id: 1120} + <750:1, 250:1>*0x7f9fe1446d20.negError{id: 1010} + <750:1, 250:1>*0x7f9fe1449b30.negError{id: 981} + <998:2>*0x7f9fe145c160:UISV-fill-proportionally.negError{id: 1003} + <998:-1, 250:-0.585365>*0x7f9fe1460980:UISV-canvas-connection.marker{id: 1019} + <998:1, 250:0.585365>*0x7f9fe1460b60:UISV-canvas-connection.marker{id: 1026} + <999:1, 998:-1, 250:-1.58537>*0x7f9fe1460d00:UISV-fill-proportionally.negError{id: 1030} + <999:1, 998:1, 250:1.58537>*0x7f9fe1460d00:UISV-fill-proportionally.posErrorMarker{id: 1029} + <998:-1, 250:-0.585366>*0x7f9fe1461610:UISV-canvas-connection.marker{id: 1048} + <998:1, 250:0.585366>*0x7f9fe1461810:UISV-canvas-connection.marker{id: 1055} + <750:1, 250:1>*0x7f9fe14618b0.negError{id: 1018} + <750:1, 250:1>*0x7f9fe1463960.negError{id: 1016} + <998:1, 250:0.585365>*0x7f9fe1464720:UISV-spacing.marker{id: 1028} + <999:1, 998:1>*0x7f9fe1464cd0:UISV-fill-proportionally.negError{id: 1001} + <999:1, 998:-1>*0x7f9fe1464cd0:UISV-fill-proportionally.posErrorMarker{id: 1000} + <750:1, 250:1>*0x7f9fe1468c90.negError{id: 1012} + <750:1, 250:1>*0x7f9fe1468e00.negError{id: 1039} + <750:1, 250:1>*0x7f9fe1469250.negError{id: 987} + <750:1, 250:1>*0x7f9fe14692b0.negError{id: 989} + <998:-1>*0x7f9fe1469960:UISV-alignment.marker{id: 1007} + <998:1>*0x7f9fe1469a80:UISV-alignment.marker{id: 1008} + <998:-1>*0x7f9fe146b870:UISV-canvas-connection.marker{id: 1004} + <998:1>*0x7f9fe146b960:UISV-canvas-connection.marker{id: 1005} + <999:-2.42144e-07, 998:-1, 250:-2>*0x7f9fe146d360:UISV-spacing.marker{id: 999} + <998:1, 250:0.585366>*0x7f9fe146eeb0:UISV-spacing.marker{id: 1057} + <999:1, 998:-1, 250:-1.58537>*0x7f9fe146efd0:UISV-fill-proportionally.negError{id: 1059} + <999:1, 998:1, 250:1.58537>*0x7f9fe146efd0:UISV-fill-proportionally.posErrorMarker{id: 1058} + <250:-2.58537>*0x7f9fe14723c0:UISV-alignment.marker{id: 1066} + <999:-2.42144e-07, 998:-1, 250:-2>*0x7f9fe1472ad0:UISV-canvas-connection.marker{id: 997} + <999:1, 998:1, 250:-1.58537>*0x7f9fe14752a0:UISV-fill-proportionally.negError{id: 1088} + <999:1, 998:-1, 250:1.58537>*0x7f9fe14752a0:UISV-fill-proportionally.posErrorMarker{id: 1087} + <998:2>*0x7f9fe1475610:UISV-fill-proportionally.negError{id: 1090} + <998:-0.999999, 250:3.58537>*0x7f9fe14758b0:UISV-canvas-connection.marker{id: 1091} + <998:0.999999, 250:-3.58537>*0x7f9fe14759a0:UISV-canvas-connection.marker{id: 1092} + <250:3.58537>*0x7f9fe14764e0:UISV-canvas-connection.marker{id: 1033} + <250:-3.58537>*0x7f9fe14765d0:UISV-canvas-connection.marker{id: 1034} + <250:2.58537>*0x7f9fe1476b90:UISV-alignment.marker{id: 1036} + <250:-2.58537>*0x7f9fe1476cb0:UISV-alignment.marker{id: 1037} + <750:1, 250:1>*0x7f9fe1478bf0.negError{id: 983} + <250:-2.62021>*0x7f9fe14798a0:UISV-spacing.marker{id: 1163} + <999:2.42144e-07, 998:1, 250:2>*0x7f9fe147a220:UISV-canvas-connection.marker{id: 990} + <998:2>*0x7f9fe147c1b0:UISV-fill-proportionally.posErrorMarker{id: 1031} + <750:1, 250:1>*0x7f9fe147ccf0.negError{id: 1041} + <750:1, 250:1>*0x7f9fe147ce60.negError{id: 1068} + <750:1, 250:1>*0x7f9fe147d120.negError{id: 1045} + <750:1, 250:1>*0x7f9fe147d180.negError{id: 1047} + <998:2>*0x7f9fe1484350:UISV-fill-proportionally.posErrorMarker{id: 1060} + <250:3.58537>*0x7f9fe14845f0:UISV-canvas-connection.marker{id: 1062} + <250:-3.58537>*0x7f9fe14846e0:UISV-canvas-connection.marker{id: 1063} + <250:2.58537>*0x7f9fe14849f0:UISV-alignment.marker{id: 1065} + <750:1, 250:1>*0x7f9fe1486c40.negError{id: 1070} + <750:1, 250:1>*0x7f9fe1486db0.negError{id: 1097} + <750:1, 250:1>*0x7f9fe1487070.negError{id: 1074} + <750:1, 250:1>*0x7f9fe14870d0.negError{id: 1076} + <998:1, 250:-0.585366>*0x7f9fe14874a0:UISV-canvas-connection.marker{id: 1077} + <998:-1, 250:0.585366>*0x7f9fe14876a0:UISV-canvas-connection.marker{id: 1084} + <998:-1, 250:0.585366>*0x7f9fe1487890:UISV-spacing.marker{id: 1086} + <250:-2.58537>*0x7f9fe14889b0:UISV-alignment.marker{id: 1124} + <750:1, 250:1>*0x7f9fe1488d40.negError{id: 1128} + <250:2.62021>*0x7f9fe1488ee0:UISV-canvas-connection.marker{id: 1154} + <750:1, 250:1>*0x7f9fe1489170.negError{id: 1132} + <750:1, 250:1>*0x7f9fe14891d0.negError{id: 1134} + <999:2.42144e-07, 998:1, 250:2>*0x7f9fe1489570:UISV-canvas-connection.marker{id: 1135} + <999:-2.42144e-07, 998:-1, 250:-2>*0x7f9fe1489770:UISV-canvas-connection.marker{id: 1142} + <999:-2.42144e-07, 998:-1, 250:-2>*0x7f9fe1489e50:UISV-spacing.marker{id: 1144} + <999:1, 998:1>*0x7f9fe1489fc0:UISV-fill-proportionally.negError{id: 1146} + <999:1, 998:-1>*0x7f9fe1489fc0:UISV-fill-proportionally.posErrorMarker{id: 1145} + <998:2>*0x7f9fe148a330:UISV-fill-proportionally.negError{id: 1148} + <998:-1>*0x7f9fe148a5d0:UISV-canvas-connection.marker{id: 1149} + <998:1>*0x7f9fe148a6c0:UISV-canvas-connection.marker{id: 1150} + <998:-1>*0x7f9fe148ac80:UISV-alignment.marker{id: 1152} + <998:1>*0x7f9fe148ada0:UISV-alignment.marker{id: 1153} + <250:-2.62021>*0x7f9fe148b200:UISV-spacing.marker{id: 1156} + <250:-2.62021>*0x7f9fe148b2c0:UISV-canvas-connection.marker{id: 1155} + <250:-2.62021>*0x7f9fe148b3e0:UISV-spacing.marker{id: 1157} + <250:-2.62021>*0x7f9fe148bbb0:UISV-spacing.marker{id: 1159} + <250:0.965157>*0x7f9fe148c4d0:UISV-fill-equally.marker{id: 1160} + <250:0.965157>*0x7f9fe148c850:UISV-fill-equally.marker{id: 1162} + <998:-0.999999, 250:0.965157>*0x7f9fe148d220:UISV-fill-equally.marker{id: 1164} + <250:-0.620209>*0x7f9fe148d270:UISV-fill-equally.marker{id: 1158} + <250:-2.62021>*0x7f9fe148d450:UISV-spacing.marker{id: 1161} + <250:-2.62021>*0x7f9fe148ff30:UISV-spacing.marker{id: 1165} + <250:2.62021>*0x7f9fe1520c60.marker{id: 919} + <250:-1.26154>*0x7f9fe15eeda0.marker{id: 917} + <250:-1.26154>*0x7f9fe50bb470:UISV-canvas-connection.marker{id: 1174} + <999:2>*0x7f9fe50c51e0.negError{id: 922} + <999:2>*0x7f9fe50cefe0.negError{id: 925} + <998:1>*0x7f9fe5113050.marker{id: 899} + <250:-1.58537>*0x7f9fe5130060.marker{id: 902} + <250:-1.58537>*0x7f9fe513af60.marker{id: 905} + <998:0.999999, 250:-1.58537>*0x7f9fe51502a0.marker{id: 908} + <250:-1.58537>*0x7f9fe5164440.marker{id: 911} + <250:-0.620209>*0x7f9fe516ba50:UISV-fill-equally.marker{id: 1171} + <998:1>*0x7f9fe517b4b0.marker{id: 914} + <250:0.965157>*0x7f9fe53e18a0:UISV-fill-equally.marker{id: 1168} + <250:0.00905983>*UIStackView:0x7f9fe1526830.Height{id: 966} + <999:0.00277778>*_UILayoutGuide:0x7f9fe53be860.Width{id: 923} + <999:0.00277778>*_UILayoutGuide:0x7f9fe53be9f0.Width{id: 926}}: internal error.  Setting empty vector for variable MyApp.StyledLabelT1:0x7f9fe53d8120'4'.Height{id: 898}.'


Auto layout internal error.  Cannot find an outgoing row head for incoming head during optimization of variable with near-zero coefficient, which should never happen.

调用堆栈:

*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e136d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000110dcedeb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e136cbd +[NSException raise:format:] + 205
    3   Foundation                          0x000000010efa7acc -[NSISObjectiveLinearExpression setPriorityVector:forKnownAbsentVariable:] + 76
    4   Foundation                          0x000000010f14ac44 __128-[NSISObjectiveLinearExpression replaceVariable:withExpression:processVariableNewToReceiver:processVariableDroppedFromReceiver:]_block_invoke + 297
    5   Foundation                          0x000000010efa46bf -[NSISLinearExpression enumerateVariablesAndCoefficients:] + 350
    6   Foundation                          0x000000010f14ab04 -[NSISObjectiveLinearExpression replaceVariable:withExpression:processVariableNewToReceiver:processVariableDroppedFromReceiver:] + 369
    7   Foundation                          0x000000010efa57ee -[NSISEngine substituteOutAllOccurencesOfBodyVar:withExpression:] + 633
    8   Foundation                          0x000000010efa8789 -[NSISEngine pivotToMakeBodyVar:newHeadOfRowWithHead:andDropRow:] + 330
    9   Foundation                          0x000000010efb2c21 -[NSISEngine removeConstraintWithMarker:] + 707
    10  Foundation                          0x000000010ef99899 -[NSLayoutConstraint _removeFromEngine:] + 229
    11  UIKit                               0x0000000110139d81 -[UIView(UIConstraintBasedLayout) _layoutEngine_willRemoveLayoutConstraint:] + 173
    12  UIKit                               0x000000011013a6b6 -[UIView(UIConstraintBasedLayout) nsli_removeConstraint:] + 114
    13  UIKit                               0x000000011013a62d -[UIView(UIConstraintBasedLayout) _removeConstraint:] + 301
    14  UIKit                               0x000000011014adde _UIViewRemoveConstraintsMadeDanglyByChangingSuperview + 1028
    15  UIKit                               0x000000010f8fc504 __45-[UIView(Hierarchy) _postMovedFromSuperview:]_block_invoke + 61
    16  UIKit                               0x000000010f8fc461 -[UIView(Hierarchy) _postMovedFromSuperview:] + 544
    17  UIKit                               0x000000010f8fa489 __UIViewWasRemovedFromSuperview + 213
    18  UIKit                               0x000000010f8f9fbf -[UIView(Hierarchy) removeFromSuperview] + 474

如果我修改方法以清除任何子UIStackViews,我100%的时间都会崩溃,如下所示:

extension UIStackView {
    func removeAllArrangedSubviews() {
        for child in self.arrangedSubviews {
            if let childStackView = child as? UIStackView {
                childStackView.removeAllArrangedSubviews()
            }

            self.removeArrangedSubview(child)
            child.removeFromSuperview()
        }
    }
}

版本:我正在运行Xcode 7.3和iOS 9.3。

0 个答案:

没有答案