我在UINavigationController视图堆栈中有一个UIView。在UIView里面我有一个UIWebView,它填充了HTML格式的文本(最初从xml文件下载并存储在coredata中)。
我希望UIWebView能够垂直扩展以适应内容,所以我称之为代码:
int screenWidth = self.view.frame.size.width - 40;
CGRect frame = descText.frame;
frame.size.height = 1;
frame.size.width = screenWidth;
descText.frame = frame;
CGSize fittingSize = [descText CGSizeZero];
frame.size = fittingSize;
在纵向模式下加载视图但在横向加载时,此方法正常。它扩大得太远,几乎翻了一番。原因是sizeThatFits返回一个太大的高度:
2011-05-05 14:16:20.268 DesignFedApp[2792:207] CURRENT SIZE: {293, 592} FITTING SIZE: {293, 592}
2011-05-05 14:16:26.282 DesignFedApp[2792:207] new orientation = 4
2011-05-05 14:16:26.283 DesignFedApp[2792:207] new orientation = 4
2011-05-05 14:16:26.283 DesignFedApp[2792:207] ROOT!! willRoatetToInterfaceOrientation!!!!!!!!!!!
2011-05-05 14:16:27.452 DesignFedApp[2792:207] CELL CLICKED: 1
2011-05-05 14:16:27.452 DesignFedApp[2792:207] LocNavController locSelected
2011-05-05 14:16:27.453 DesignFedApp[2792:207] Loc: The Hood
2011-05-05 14:16:27.459 DesignFedApp[2792:207] should rotate to LANDSCAPE
2011-05-05 14:16:27.481 DesignFedApp[2792:207] POSITION ELEMENTS view.width {{0, 0}, {480, 268}}
2011-05-05 14:16:27.483 DesignFedApp[2792:207] CURRENT SIZE: {440, 761} FITTING SIZE: {440, 761}
真正奇怪的是,当加载的HTML只包含1个p标签时,这不会发生!多个
标签,我们遇到了上述问题。但只在景观中。
所以要重新上限。 UIWebView sizeThatFits返回不正确的高度,但仅在横向模式下,且仅当html具有多个p标记时才会返回。
如果有人能够对此有所了解它可以节省我数周的时间,我一直在阅读这么多的论坛和所有的Apple文档,但没有找到任何可以帮助我解决这个具体案例。
先谢谢你,伊恩。
编辑:确定我注意到只有在加载的HTML字符串中设置了多个标记时才会出现问题:
NSString *html=@"<p>This is a parapgrah this will work fine......</p>";
NSString *html2=@"<p>This is a paragraph with other <i>tags</i>, this wont work properly</p>";
整体方法。从webViewDidFinishLoad:webView
调用-(void)positionElements:(BOOL)inLandScape{
NSLog(@"position elements");
//stretch frame to fit content
int screenWidth = self.view.frame.size.width - 40;
CGRect frame = descText.frame;
frame.size.height = 1;
frame.size.width = screenWidth;
descText.frame = frame;
CGSize fittingSize = [descText sizeThatFits:CGSizeZero];
NSLog(@"fittingSize: %@",NSStringFromCGSize(fittingSize));
frame.size = fittingSize;
descText.frame = frame;
//position buttons underneath
CGRect mapBtnFrame = showOnMapBtn.frame;
mapBtnFrame.origin.y = frame.origin.y + frame.size.height + 20;
showOnMapBtn.frame = mapBtnFrame;
CGRect locBtnFrame = locationInfoBtn.frame;
locBtnFrame.origin.y = mapBtnFrame.origin.y + mapBtnFrame.size.height + 20;
locationInfoBtn.frame = locBtnFrame;
CGRect scrollFrame = self.view.frame;
scrollFrame.size.height -= 49;
container.frame = scrollFrame;
CGRect contentRect = CGRectZero;
for (UIView *containerSubView in container.subviews)
contentRect = CGRectUnion(contentRect, containerSubView.frame);
container.contentSize = contentRect.size;
}
答案 0 :(得分:0)
如果您使用UINavigationController
并希望UIWebView
占据整个内容区域,则根本不需要手动设置框架。您应该只能在[view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight];
和UIView
子视图上致电UIWebView
。