ios - 自定义 UIVIew layoutSubviews 在设置转换时会导致错误(AutoLayout 要求我调用 super 方法,即使我已经调用了)

标签 ios uiview autolayout layoutsubviews

我遇到了一个奇怪的问题,我试图覆盖 -layoutSubviews 以在横向中创建不同的布局。我的方法在纵向上运行良好,但是当我在横向的 subview 之一上设置变换时,出现此错误:

 *** Assertion failure in -[LSSlideNotesView layoutSublayersOfLayer:], /SourceCache/UIKit/UIKit-2380.17/UIView.m:5776
 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Auto Layout still required after executing -layoutSubviews. LSSlideNotesView's implementation of -layoutSubviews needs to call super.'

我的方法如下:

-(void)layoutSubviews {
    [super layoutSubviews];
    bool horz = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    if (horz) {
        CGRect contentFrame = self.bounds;
        contentFrame.size.width -= (notesHeight + 40);
        if (contentFrame.size.width < 0) {
            contentFrame.size.width = 0;
            notesHeight = self.bounds.size.width - 40;
        }
        slideCarouselView.frame = contentFrame;
        remotePointerView.frame = contentFrame;

        // ***** This is the offending line *****
        notesLabel.transform = CGAffineTransformMakeRotation(-M_PI_2);
        // **************************************
        notesLabel.bounds = CGRectMake(0, 0, contentFrame.size.height, 40);
        notesLabel.center = CGPointMake(CGRectGetMaxX(contentFrame) + 20, contentFrame.size.height / 2);

        CGRect notesRect = self.bounds;
        notesRect.origin.x = notesRect.size.width - notesHeight;
        notesRect.size.width = notesHeight;
        notesTextView.frame = notesRect;
        labelCopyFail.frame = CGRectMake(contentFrame.size.width / 2 - 65, contentFrame.size.height * 3 / 4 - 20, 130, 30);
    } else {
        CGRect contentFrame = self.bounds;
        contentFrame.size.height -= (notesHeight + 40 + keyboardHeight);
        if (contentFrame.size.height < 0) {
            contentFrame.size.height = 0;
            notesHeight = self.bounds.size.height - keyboardHeight - 40;
        }
        slideCarouselView.frame = contentFrame;
        remotePointerView.frame = contentFrame;

        notesLabel.transform = CGAffineTransformIdentity;
        notesLabel.frame = CGRectMake(CGRectGetMinX(contentFrame), CGRectGetMaxY(contentFrame), contentFrame.size.width, 40);

        CGRect notesRect = self.bounds;
        notesRect.origin.y = notesRect.size.height - notesHeight - keyboardHeight;
        notesRect.size.height = notesHeight;
        notesTextView.frame = notesRect;
        labelCopyFail.frame = CGRectMake(contentFrame.size.width / 2 - 65, contentFrame.size.height * 3 / 4 - 20, 130, 30);
    }
}

如果我注释掉该行,就不会收到错误。有谁知道为什么会发生这种情况?我错过了什么吗?我在我的应用程序的其他地方几乎完成了这种精确的转换,并且效果很好。我一生都无法弄清楚这里发生了什么。我愿意尝试几乎任何事情。

最佳答案

嗯,这并不是一个完整的答案,但我想记录我的修复,因为它确实揭示了有关问题的更多信息,并且可能会帮助某人解决问题。

事实证明,这个问题在某种程度上与界面构建器有关(我认为)。我通过 Storyboard实例化了上述 UIVIew 的子类(通过更改 View Controller 中 Root View 的类类型)。我还将 Storyboard中的 subview 链接到 View 中的 IBOutlets 。显然,这做了一些有趣的事情,可能会覆盖或更改一些默认布局机制。

一旦我将 View 完全从界面生成器中拉出来并实例化并在代码中手动添加所有内容,它就可以正常工作。

关于ios - 自定义 UIVIew layoutSubviews 在设置转换时会导致错误(AutoLayout 要求我调用 super 方法,即使我已经调用了),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18791861/

相关文章:

ios - 尝试在 NSUserDefaults 中存储 nsdictionaries 数组时出错

iphone - Objective-C If 语句比较 float 不起作用

ios - 自动布局 Xcode。 iPhone 模拟器与 Xcode 预览不匹配

swift - cocoa 图形故障

iOS 7/8 UITableView 单元格 : Two UILabels with dynamic height with auto layout for variable row height

ios - 缩放时如何在 UIView 周围保持一致的 2px 边框?

iphone - 如何选择名称为 Object 的 UIImageView

ios - 什么使元素可访问?为什么点击手势比 UIButton 更难访问?我如何解决它?

ios - 如果你使用 set RootViewController 是否会从内存中删除之前的 RootViewController?

iphone - 如何在 iphone 中创建具有 Marquee Effect 的动态多个 uiview