ios - 调用 "[super layoutSubviews]"抛出 "[__NSArrayM objectAtIndex:]: index 1 beyond bounds"异常

标签 ios iphone ios7 uinavigationbar

每当我调用 [super layoutSubviews];在我的自定义 UINavigation 栏上它抛出
* 由于未捕获的异常“NSRangeException”而终止应用程序,原因:“* -[__NSArrayM objectAtIndex:]: 索引 1 超出范围 [0 .. 0]'

这些是我重写的仅有的两种方法:

-(void) layoutSubviews
{
    [super layoutSubviews];

    for (UIView *view in self.subviews)
    {
        CGRect frame = view.frame;
        frame.origin.y = 5;
        view.frame = frame;
    }
}

- (CGSize)sizeThatFits:(CGSize)size {
    [self setTitleVerticalPositionAdjustment:-7 forBarMetrics:UIBarMetricsDefault];
    CGRect frame = [UIScreen mainScreen].applicationFrame;
    CGSize newSize = CGSizeMake(frame.size.width , 64);
    return newSize;
}

该应用程序正在使用 Storyboard ,并且我在加载 View 时向 UINavigationBar 动态添加内容,但是即使我禁用此内容注入(inject),仍然会抛出错误,这似乎是调用 [super layoutSubviews] 的行为;这导致了这个异常,

有没有人知道为什么会发生这种情况?

您可以看到,在引发异常后,所有项目都已正确定位在 UINavigationBar 中。

亲切的问候,

帕特里克·J·奎因。

最佳答案

从文档...

You should not call this method directly. If you want to force a layout update, call the setNeedsLayout method instead to do so prior to the next drawing update. If you want to update the layout of your views immediately, call the layoutIfNeeded method.



你应该用...
[super setNeedsLayout];

如果你需要调用它,我怀疑你真的这样做了。

关于ios - 调用 "[super layoutSubviews]"抛出 "[__NSArrayM objectAtIndex:]: index 1 beyond bounds"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148263/

相关文章:

ios - 从 UIViewController 中的 SwiftUIView 分配 @state var

ios - 如何在 sencha touch 中从 css 文件获取最高值到 .js Controller ?

iphone - 如何为 CGPoint 线赋予独特的颜色 Xcode

ios7 - 如何关闭在iOS7中打开的所有Alertviews

objective-c - 带有 ModalPresentationStyle 的 Popover 在 iOS 7 iPad 中不居中

ios - 动态单元格大小

ios - "Connect via network"无线调试不工作 Xcode 9

iphone - 插入 subview :belowSubview: in UITableViews

iOS:NSUserDefault 在 AppDelegate 中加载速度不够快?条件间歇性失败

c++ - 为设备编译 Boost 1.54 XCode5 - fatal error : error in backend: symbol '___umodsi3' can not be undefined in a subtraction expression