当前位置: 首页 > 工具软件 > Address Bar > 使用案例 >

UIWebView AddressBar Scroll

彭烨烁
2023-12-01
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
 类似资料:

相关阅读

相关文章

相关问答