开发者

UIWebView sizeThatFits incorrect in Landscape mode, multiple paragpraphs

开发者 https://www.devze.com 2023-03-02 18:29 出处:网络
I Have a UIView in a UINavigationController view stack. Inside the UIView I have a UIWebView, which gets populated with HTML formatted text (originally download from an xml file and stored in coredata

I Have a UIView in a UINavigationController view stack. Inside the UIView I have a UIWebView, which gets populated with HTML formatted text (originally download from an xml file and stored in coredata).

I want the UIWebView to expand vertically to fit the content so I call this code:

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;

This works fine when the View is loaded in Portrait mode but when loaded in landscape. it expands too far, nearly double. The reason for this is that sizeThatFits returns a height that is far too big:

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}

The really weird thing is t开发者_开发问答hat this does not happen when the HTML being loaded only consists of 1 p tag!!!! Multiple

tags and we have the issue described above. But only in Landscape.

So to re-cap. UIWebView sizeThatFits return incorrect height, but only in landscape mode and only when html has more than one p tag.

If anyone can shed any light on this it could save me weeks of time, I've been reading though so many forums and all the Apple docs but haven't found anything that can help me with this specific case.

Thanks in advance, Ian.

EDIT: OK I've noticed that the problem only occurs if the loaded HTML String has more than 1 tag set in it:

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>";

Whole Method. Called from 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;
    }


If you're using a UINavigationController and you want your UIWebView to take up the whole content area, you shouldn't need to manually set the frame at all. You should just be able to call [view setAutoresizingMask:UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight]; on both the UIView and the UIWebView subview.

0

精彩评论

暂无评论...
验证码 换一张
取 消