BrowserView.h
#import <UIKit/UIKit.h>
@interface BrowserView : UIView <UIWebViewDelegate, UIScrollViewDelegate> {
UINavigationBar *addressBar_;
UIWebView *webView_;
}
@property (nonatomic, readonly) UINavigationBar *addressBar;
@property (nonatomic, readonly) UIWebView *webView;
@end
BrowserView.m
#import ”BrowserView.h”
@interface BrowserView(Private)
- (void)clearAddressBar;
- (void)clearWebView;
@end
#define kHeightOfAddressBar 52.0f
@implementation BrowserView
@synthesize addressBar = addressBar_;
@synthesize webView = webView_;
#pragma mark - Initialize
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self clearAddressBar];
CGRect frameOfAddressBar = CGRectMake(0, -kHeightOfAddressBar, self.frame.size.width, kHeightOfAddressBar);
addressBar_ = [[UINavigationBar alloc] initWithFrame:frameOfAddressBar];
addressBar_.autoresizingMask = (UIViewAutoresizingFlexibleWidth);
[self clearWebView];
webView_ = [[UIWebView alloc] initWithFrame:self.bounds];
webView_.delegate = self;
webView_.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
for (id subview in webView_.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
scrollView.delegate = self;
scrollView.contentInset = UIEdgeInsetsMake(frameOfAddressBar.size.height, 0, 0, 0);
[scrollView setContentOffset:CGPointMake(0, frameOfAddressBar.origin.y) animated:NO];
[scrollView addSubview:addressBar_];
}
}
[webView_ loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@”http://www.google.com”]]];
[self addSubview:webView_];
}
return self;
}
#pragma mark - Memory management
- (void)dealloc
{
[self clearAddressBar];
[self clearWebView];
[super dealloc];
}
#pragma mark - Address Bar
- (void)clearAddressBar
{
[addressBar_ release], addressBar_ = nil;
}
#pragma mark - Web view
- (void)clearWebView
{
if (webView_ != nil) {
webView_.delegate = nil;
}
[webView_ release], webView_ = nil;
}
#pragma mark - Web view delegate
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
for (id subview in webView.subviews) {
if ([subview isKindOfClass:[UIScrollView class]]) {
UIScrollView *scrollView = (UIScrollView *)subview;
[scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}
}
}
#pragma mark - Scroll view delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
if (scrollView.contentOffset.y < 0) {
UIEdgeInsets insects = scrollView.scrollIndicatorInsets;
insects.top = -scrollView.contentOffset.y;
scrollView.scrollIndicatorInsets = insects;
}
}
@end