我在我的Cocoa应用程序中添加了一个WebView,它产生了一个错误

小弟调*_*弟调调 11 macos xcode cocoa objective-c

我在我的应用程序中添加了一个WebView,并使用以下代码将页面加载到其中:

-(void)awakeFromNib{
    NSString *resourcesPath = [[NSBundle mainBundle] resourcePath];
    NSString *htmlPath = [resourcesPath stringByAppendingString:@"/calendarHTML/test.html"];
    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:htmlPath]]]; 
}
Run Code Online (Sandbox Code Playgroud)

但是,当我运行该应用程序时,我收到以下错误:

Layout still needs update after calling -[WebHTMLView layout].  
WebHTMLView or one of its superclasses may have overridden 
-layout without calling super. Or, something may have dirtied 
layout in the middle of updating it.  Both are programming errors in 
Cocoa Autolayout.  The former is pretty likely to arise if some 
pre-Cocoa Autolayout class had a method called layout, but it should be fixed.!
Run Code Online (Sandbox Code Playgroud)

错误日志

是什么导致了这个问题?

Rob*_*ger 12

这是因为包含您放置WebView的窗口的笔尖正在使用Lion中引入的新的自动布局功能.

当nib文件启用了自动布局时,窗口将在窗口中的-layout所有NSView对象上调用该方法.

这会导致一个问题WebView,因为它有一个名为方法-layout之前,它被添加到该方法NSView在狮子API和WebViewlayout方法并不了解自动布局.

可能目前最好的解决方法是使用旧的自动调整掩码方法在窗口中布局视图.由于Xcode现在创建启用了autolayout的nib文件,您需要自己禁用它.

您可以在文件检查器中为nib文件执行此操作,方法是禁用该Use Auto Layout复选框.

界面构建器检查员

执行此操作后,您需要确保在视图检查器的"大小"选项卡中,笔尖中的所有视图都具有正确的自动调整大小设置.