每当我调用 [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/