An alternative solution to a very strange problem about uiwebview (high increment problem)

Source: Internet
Author: User

Recently, an app on an iPad uses uiwebview to display advertisements. There are two different forms of advertisements: vertical screen and horizontal screen. The processing method is not difficult. Create an advertisement webview, set the frame, and add it to the view. When rotating, load the corresponding ad HTML address, and then set the webview frame. A few codes, functions are implemented, and tests are passed. But if you rotate it several times, you will find a strange problem. The advertisement HTML will be stretched continuously and the height will grow. The output log shows that the height in contentsize in webview increases by 8 pixels each time. It turns out this is the reason. It is estimated that the width of the webview changes during rotation, so the height scales slowly. Find the cause and reset contentsize. There is often a gap between facts and imagination. The problems remain in the first test. This is dumb, and the contentsize is reset, but it is completely ineffective. Think about whether there are any attributes that can control whether the content is scaled or not. I searched for them and did not find the corresponding attributes. Various attributes have been tried, including scalespagetofit. Using stringbyevaluatingjavascriptfromstring to set the contentsize height and reload using JavaScript statements, the problem persists and cannot be solved.

When you are confused, check whether the subview is set incorrectly. Previously, I tried to directly set the frame of the scrollview of uiwebview, but I didn't see any results. Will there be other subviews. So I tried to traverse all the subviews of the uiwebview. I did not intend to find a subview OF THE uiwebbrowserview. So I tried to print the size of the view and modify it to the desired size. I was surprised to find that the previously increasing 8 pixels was what it was added. After resetting the size, stretching never occurs again. Shit couldn't help but make a rough remark. I couldn't think of it, but it would take so much effort to change the size. Is I understand not much or the API of the Bug, do not understand, before reading information, also saw someone asked a similar problem (problem address: http://www.devdiv.com/forum.php? MoD = viewthread & tid = 194580), but there is no solution. I hope that you will learn about the Orthodox solution to this problem later. I also hope that some experts can give you some advice to solve this question.

The solution code is as follows:

- (void)fitContentForWebviewResize{    for (UIView *subview in m_adView.subviews)    {        NSLog(@"subview class %@", NSStringFromClass([subview class]));        if ([[subview class] isSubclassOfClass: [UIScrollView class]])        {            for (UIView *subview2 in subview.subviews)            {                NSLog(@"subview class %@", NSStringFromClass([subview2 class]));                if ([[subview2 class] isSubclassOfClass: NSClassFromString(@"UIWebBrowserView")])                {                    CGRect scrollFrame = subview2.frame;                    NSLog(@"%@", NSStringFromCGRect(scrollFrame));                    subview2.frame = CGRectMake(0, 0, m_adView.frame.size.width, 100);                    break;                }            }            CGRect scrollFrame = subview.frame;            NSLog(@"%@", NSStringFromCGRect(scrollFrame));            break;        }    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.