• <small id='p6Zxx'></small><noframes id='p6Zxx'>

  • <i id='p6Zxx'><tr id='p6Zxx'><dt id='p6Zxx'><q id='p6Zxx'><span id='p6Zxx'><b id='p6Zxx'><form id='p6Zxx'><ins id='p6Zxx'></ins><ul id='p6Zxx'></ul><sub id='p6Zxx'></sub></form><legend id='p6Zxx'></legend><bdo id='p6Zxx'><pre id='p6Zxx'><center id='p6Zxx'></center></pre></bdo></b><th id='p6Zxx'></th></span></q></dt></tr></i><div id='p6Zxx'><tfoot id='p6Zxx'></tfoot><dl id='p6Zxx'><fieldset id='p6Zxx'></fieldset></dl></div>

    1. <legend id='p6Zxx'><style id='p6Zxx'><dir id='p6Zxx'><q id='p6Zxx'></q></dir></style></legend>
        <bdo id='p6Zxx'></bdo><ul id='p6Zxx'></ul>

    2. <tfoot id='p6Zxx'></tfoot>

        应用变换时自动布局崩溃:-[layoutSublayersOfLayer:] 中的断言失败

        时间:2023-09-10

            <small id='0rJBl'></small><noframes id='0rJBl'>

              <bdo id='0rJBl'></bdo><ul id='0rJBl'></ul>

                1. <legend id='0rJBl'><style id='0rJBl'><dir id='0rJBl'><q id='0rJBl'></q></dir></style></legend>
                  <tfoot id='0rJBl'></tfoot>
                    <tbody id='0rJBl'></tbody>
                  <i id='0rJBl'><tr id='0rJBl'><dt id='0rJBl'><q id='0rJBl'><span id='0rJBl'><b id='0rJBl'><form id='0rJBl'><ins id='0rJBl'></ins><ul id='0rJBl'></ul><sub id='0rJBl'></sub></form><legend id='0rJBl'></legend><bdo id='0rJBl'><pre id='0rJBl'><center id='0rJBl'></center></pre></bdo></b><th id='0rJBl'></th></span></q></dt></tr></i><div id='0rJBl'><tfoot id='0rJBl'></tfoot><dl id='0rJBl'><fieldset id='0rJBl'></fieldset></dl></div>
                2. 本文介绍了应用变换时自动布局崩溃:-[layoutSublayersOfLayer:] 中的断言失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个在右下角显示 UILabel 的自定义视图.视图是在从 initWithCoder:initWithFrame: 调用的方法中设置的,如下所示:

                  I have a custom view that display a UILabel in the bottom right corner. The view is setup in a method called from both initWithCoder: and initWithFrame: like this:

                  MCLabel* likesLabel = [[MCLabel alloc] init];
                  likesLabel.mc_textPadding = UIEdgeInsetsMake(0, 10, 0, 10);
                  likesLabel.font = [UIFont fontWithName:@"FontAwesome" size:12.f];
                  [likesLabel setText:@"test"];
                  likesLabel.numberOfLines = 2;
                  likesLabel.backgroundColor = [UIColor colorWithWhite:1 alpha:.8];
                  likesLabel.textColor = UIColor.blackColor;
                  likesLabel.translatesAutoresizingMaskIntoConstraints = NO;
                  likesLabel.textAlignment = NSTextAlignmentCenter;
                  likesLabel.mc_verticalTextAlignment = MCVerticalTextAlignmentTop;
                  
                  [self addSubview:likesLabel];
                  self.likesLabel = likesLabel;
                  
                  NSLayoutConstraint* widthConstraint = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeWidth multiplier:1 constant:1];
                  NSLayoutConstraint* heightConstraint = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:likesLabel attribute:NSLayoutAttributeWidth multiplier:2/5.f constant:1];
                  NSLayoutConstraint* horizontalPosition  = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeRight multiplier:1 constant:1];
                  NSLayoutConstraint* verticalPosition  = [NSLayoutConstraint constraintWithItem:likesLabel attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self attribute:NSLayoutAttributeBottom multiplier:1 constant:1];
                  [likesLabel addConstraints:@[heightConstraint]];
                  [self addConstraints:@[widthConstraint, horizontalPosition, verticalPosition]];
                  

                  现在,如果我像这样留下所有内容,我不会有任何问题,但是,只要我对此标签应用变换(这是 UILabel 的子类,如果重要,只需添加垂直对齐和边缘插入)应用程序因控制台中的错误而崩溃:

                  Now if I leave everything like this I do not have any kind of problem but, as soon as I apply a transform to this label (that is a subclass of UILabel that simply add vertical alignment and edge insets if that matters) the app crashes with the error in console:

                  *** Assertion failure in -[MCView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2935.138/UIView.m:8794
                  Auto Layout still required after executing -layoutSubviews
                  

                  断言提示可能子类在覆盖方法时没有调用 [super layoutSubviews] 但我确实调用了.

                  The assertion hints that probably the subclass didn't call [super layoutSubviews] when overriding the methods but I did.

                  由于很明显这里的问题是自动布局设置,我担心我忽略了一些东西,也许布局不明确,因此崩溃.

                  Since is clear that the problem here is the autolayout setup I'm afraid that I'm overlooking at something and maybe the layout is ambiguous hence the crash.

                  另一个注意事项:如果我在 - (void)didMoveToSuperview 方法中移动转换,相同的代码不会在 iOS 8 上崩溃.

                  One more note: the same code does not crash on iOS 8 if I move the transform in the - (void)didMoveToSuperview method.

                  有谁可以帮忙吗?

                  推荐答案

                  我在 iOS7(但不是 iOS8)中遇到了这个问题,原因是在 viewWillLayoutSubviews 中进行了约束调整.将约束调整移动到 viewWillAppear 为我修复了它.

                  I had this issue in iOS7 (but not iOS8) from putting constraint adjustments in viewWillLayoutSubviews. Moving constraint adjustment to viewWillAppear fixed it for me.

                  这篇关于应用变换时自动布局崩溃:-[layoutSublayersOfLayer:] 中的断言失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何强制“尊重语言方向"从 RTL 到 LTR,反之亦然 下一篇:无法在 iOS 中使视图与单元格宽度相等?

                  相关文章

                3. <i id='FiiG9'><tr id='FiiG9'><dt id='FiiG9'><q id='FiiG9'><span id='FiiG9'><b id='FiiG9'><form id='FiiG9'><ins id='FiiG9'></ins><ul id='FiiG9'></ul><sub id='FiiG9'></sub></form><legend id='FiiG9'></legend><bdo id='FiiG9'><pre id='FiiG9'><center id='FiiG9'></center></pre></bdo></b><th id='FiiG9'></th></span></q></dt></tr></i><div id='FiiG9'><tfoot id='FiiG9'></tfoot><dl id='FiiG9'><fieldset id='FiiG9'></fieldset></dl></div>
                  <legend id='FiiG9'><style id='FiiG9'><dir id='FiiG9'><q id='FiiG9'></q></dir></style></legend>

                    1. <small id='FiiG9'></small><noframes id='FiiG9'>

                      <tfoot id='FiiG9'></tfoot>

                        <bdo id='FiiG9'></bdo><ul id='FiiG9'></ul>