开发者

Iphone: Adding a "footer" to the bottom of a UIWebView

开发者 https://www.devze.com 2023-04-05 03:17 出处:网络
I\'m trying to add an UIButton at the bottom of the webView. What would be the best开发者_如何学JAVA way to accomplish this?

I'm trying to add an UIButton at the bottom of the webView. What would be the best开发者_如何学JAVA way to accomplish this?

I'm thinking of putting the webview in a tableview and then adding a footer to the tableview, but I'm hoping there's an easier solution.


Create UIView, add UIWebView and UIButton as its subviews:

  • UIView
    • UIWebView
    • UIButton


I figured out a solution by using KVO on webView.scrollView.contentSize.

I've blogged about it here http://objectivesea.tumblr.com/post/58540464993/adding-a-footer-to-uiwebview and all the code needed can be found in this commit: https://github.com/coryalder/DMAFWebViewController/commit/07032756f3e72f62f86b077319c44d3f5d7cab6d

Basically, you watch contentSize for changes, and when it changes you increase the contentSize by footerView.height and set the frame on footerView to place it at the bottom of the scrollView. Note that this means you have to watch for recursive contentSize calls. I do this by recording the target contentSize as an associatedObject (objc_setAssociatedObject), but you could just as easily store it as a property.


Use this code to add button at the bottom of webview.

CGSize size = webView.scrollView.contentSize;

button.frame = CGRectMake(button.frame.origin.x, size.height, button.frame.size.width, button.frame.size.height);
[webView.scrollView addSubview:button];
[webView.scrollView setContentSize:CGSizeMake(size.width, size.height + button.frame.size.height)];


If you want to make the footer follow the web view content, you have to use UIScrollViewDelegate.

There is some tricks.

  1. Get the UIScrollView in the UIWebView. It is much easy in iOS5 now.
  2. Set the delegate of UIScrollView to your delegate.
  3. Your delegate should pass all the msg from the UIScrollViewDelegate to the UIWebView.
  4. Change the position of your footer according to the contentOffset in UIScrollView
  5. Restore the delegate value, when you release the UIWebView

You can add the following code to passthrough the msg from UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
        // layout your footer here
    [self layoutAccessoryViews:scrollView];
    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidScroll:)] ) {
        [scrollViewPassthrough_ scrollViewDidScroll:scrollView];
    }
}
                                             // any offset changes
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidZoom:)] ) {
        [scrollViewPassthrough_ scrollViewDidZoom:scrollView];
    }
}

// called on start of dragging (may require some time and or distance to move)
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewWillBeginDragging:)] ) {
        [scrollViewPassthrough_ scrollViewWillBeginDragging:scrollView];
    }
}

// called on finger up if the user dragged. velocity is in points/second. targetContentOffset may be changed to adjust where the scroll view comes to rest. not called when pagingEnabled is YES
- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewWillEndDragging:withVelocity:targetContentOffset:)] ) {
        [scrollViewPassthrough_ scrollViewWillEndDragging:scrollView withVelocity:velocity targetContentOffset:targetContentOffset];
    }
}

// called on finger up if the user dragged. decelerate is true if it will continue moving afterwards
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidEndDragging:willDecelerate:)] ) {
        [scrollViewPassthrough_ scrollViewDidEndDragging:scrollView willDecelerate:decelerate];
    }
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewWillBeginDecelerating:)] ) {
        [scrollViewPassthrough_ scrollViewWillBeginDecelerating:scrollView];
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidEndDecelerating:)] ) {
        [scrollViewPassthrough_ scrollViewDidEndDecelerating:scrollView];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidEndScrollingAnimation:)] ) {
        [self scrollViewDidEndScrollingAnimation:scrollView];
    }
}

- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(viewForZoomingInScrollView:)] ) {
        return [scrollViewPassthrough_ viewForZoomingInScrollView:scrollView];
    }
    return nil;
}

- (void)scrollViewWillBeginZooming:(UIScrollView *)scrollView withView:(UIView *)view {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewWillBeginZooming:withView:)] ) {
        [scrollViewPassthrough_ scrollViewWillBeginZooming:scrollView withView:view];
    }
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidEndZooming:withView:atScale:)] ) {
        [scrollViewPassthrough_ scrollViewDidEndZooming:scrollView withView:view atScale:scale];
    }
}

- (BOOL)scrollViewShouldScrollToTop:(UIScrollView *)scrollView {

    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewShouldScrollToTop:)] ) {
        [scrollViewPassthrough_ scrollViewShouldScrollToTop:scrollView];
    }
    return YES;
}

- (void)scrollViewDidScrollToTop:(UIScrollView *)scrollView {

    [self layoutAccessoryViews:scrollView];
    if( [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidScrollToTop:)] ) {
        [scrollViewPassthrough_ respondsToSelector:@selector(scrollViewDidScrollToTop:)];
    }
}
0

精彩评论

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