Permalink
Browse files

Adding the toolbar to the WebElement, with back/forward/reload/out to…

… safari
  • Loading branch information...
1 parent b1d1efd commit 56d4146eff2a5ed87ec1f561982117f01cab348a @escoz escoz committed Aug 31, 2011
Showing with 101 additions and 4 deletions.
  1. +101 −4 quickdialog/QWebViewController.m
View
105 quickdialog/QWebViewController.m
@@ -14,36 +14,89 @@
#import "QWebViewController.h"
-@implementation QWebViewController
+@interface QWebViewController ()
+- (CGImageRef)createBackArrowImageRef;
+- (CGImageRef)createForwardArrowImageRef;
+
+@end
+
+@implementation QWebViewController {
+ UIBarButtonItem * _btBack;
+ UIBarButtonItem * _btForward;
+ BOOL _firstPageFinished;
+ BOOL _previousToolbarState;
+}
- (QWebViewController *)initWithUrl:(NSString *)url {
self = [super init];
if (self!=nil){
_webView = [[UIWebView alloc] init];
_webView.delegate = self;
- _webView.scalesPageToFit = YES;
-
+ _webView.scalesPageToFit = YES;
_url = url;
self.view = _webView;
+
+ UIImage *backImage = [[UIImage alloc] initWithCGImage:[self createBackArrowImageRef]];
+ UIImage *forwardImage = [[UIImage alloc] initWithCGImage:[self createForwardArrowImageRef]];
+ _btBack = [[UIBarButtonItem alloc] initWithImage:backImage style:UIBarButtonItemStylePlain target:self action:@selector(actionRewind)];
+ _btForward = [[UIBarButtonItem alloc] initWithImage:forwardImage style:UIBarButtonItemStylePlain target:self action:@selector(actionForward)];
+
+ _btBack.enabled = NO;
+ _btForward.enabled = NO;
}
return self;
}
- (void)viewWillDisappear:(BOOL)animated {
[_webView stopLoading];
+ [self.navigationController setToolbarHidden:_previousToolbarState animated:YES];
_webView = nil;
}
-
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
return YES;
}
+- (void)actionRewind {
+ [_webView goBack];
+ _btForward.enabled = YES;
+
+}
+
+- (void)actionForward {
+ [_webView goForward];
+}
+
+- (void)actionRefresh {
+ [_webView reload];
+}
+
+- (void)actionGoToSafari {
+ [[UIApplication sharedApplication] openURL:[_webView.request mainDocumentURL]];
+}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_url]]];
+ _previousToolbarState = self.navigationController.toolbarHidden;
+ self.navigationController.toolbarHidden = NO;
+
+
+ UIBarButtonItem *spacer1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
+ spacer1.width = 30;
+ UIBarButtonItem *spacer2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
+ spacer2.width = 30;
+ self.toolbarItems = [NSArray arrayWithObjects:
+ _btBack,
+ spacer1,
+ _btForward,
+ [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
+ [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(actionRefresh)],
+ spacer2,
+ [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(actionGoToSafari)],
+ nil];
+
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
@@ -55,11 +108,15 @@ - (void)webViewDidStartLoad:(UIWebView *)webView {
[indicator startAnimating];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:indicator];
self.title = @"Loading";
+ if (_firstPageFinished==YES){
+ _btBack.enabled = YES;
+ }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
self.navigationItem.rightBarButtonItem = nil;
self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
+ _firstPageFinished = YES;
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
@@ -69,4 +126,44 @@ - (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
}
+- (CGContextRef)createContext
+{
+ CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
+ CGContextRef context = CGBitmapContextCreate(nil,27,27,8,0, colorSpace,kCGImageAlphaPremultipliedLast);
+ CFRelease(colorSpace);
+ return context;
+}
+
+- (CGImageRef)createBackArrowImageRef
+{
+ CGContextRef context = [self createContext];
+ CGColorRef fillColor = [[UIColor blackColor] CGColor];
+ CGContextSetFillColor(context, (CGFloat *) CGColorGetComponents(fillColor));
+ CGContextBeginPath(context);
+ CGContextMoveToPoint(context, 8.0f, 13.0f);
+ CGContextAddLineToPoint(context, 24.0f, 4.0f);
+ CGContextAddLineToPoint(context, 24.0f, 22.0f);
+ CGContextClosePath(context);
+ CGContextFillPath(context);
+ CGImageRef image = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ return image;
+}
+
+- (CGImageRef)createForwardArrowImageRef
+{
+ CGContextRef context = [self createContext];
+ CGColorRef fillColor = [[UIColor blackColor] CGColor];
+ CGContextSetFillColor(context, (CGFloat *) CGColorGetComponents(fillColor));
+ CGContextBeginPath(context);
+ CGContextMoveToPoint(context, 24.0f, 13.0f);
+ CGContextAddLineToPoint(context, 8.0f, 4.0f);
+ CGContextAddLineToPoint(context, 8.0f, 22.0f);
+ CGContextClosePath(context);
+ CGContextFillPath(context);
+ CGImageRef image = CGBitmapContextCreateImage(context);
+ CGContextRelease(context);
+ return image;
+}
+
@end

0 comments on commit 56d4146

Please sign in to comment.