Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Package up for a trip. Sorry the reasons why each patch is made are b…
…eing merged and forgotten.
  • Loading branch information
adonoho committed Jun 27, 2014
commit 4a083af14eaf59836cdadc3886f1a575a68476b1
7 changes: 3 additions & 4 deletions SVWebViewController/SVModalWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

#import <UIKit/UIKit.h>

@class SVWebViewController;

@interface SVModalWebViewController : UINavigationController
@interface SVModalWebViewController : UINavigationController <UIToolbarDelegate>

- (id)initWithAddress:(NSString*)urlString;
- (id)initWithURL:(NSURL *)URL;

@property (nonatomic, strong) UIColor *barsTintColor;
@property (nonatomic) UIColor *barsTintColor;
@property (getter = isBarAttached, nonatomic) BOOL barAttached;

@end
11 changes: 11 additions & 0 deletions SVWebViewController/SVModalWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,15 @@ - (void)viewWillAppear:(BOOL)animated {
self.navigationBar.tintColor = self.barsTintColor;
}


#pragma mark - UIToolbarDelegate methods.


- (UIBarPosition) positionForBar: (id<UIBarPositioning>) bar {

return self.isBarAttached ? UIBarPositionTopAttached : UIBarPositionTop;

} // -positionForBar:


@end
26 changes: 13 additions & 13 deletions SVWebViewController/SVWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,7 @@
//
// https://github.com/samvermette/SVWebViewController

#import "SVModalWebViewController.h"

@protocol SVWebViewControllerDelegate <NSObject>

@optional

- (void) webViewControllerWillAppear: (SVWebViewController *) wvc;
- (void) webViewControllerDidAppear: (SVWebViewController *) wvc;

- (void) webViewControllerWillDisappear: (SVWebViewController *) wvc;
- (void) webViewControllerDidDisappear: (SVWebViewController *) wvc;

@end
@protocol SVWebViewControllerDelegate;

@interface SVWebViewController : UIViewController

Expand All @@ -32,3 +20,15 @@
- (void) doneButtonClicked: (id) sender;

@end

@protocol SVWebViewControllerDelegate <NSObject>

@optional

- (void) webViewControllerWillAppear: (SVWebViewController *) wvc;
- (void) webViewControllerDidAppear: (SVWebViewController *) wvc;

- (void) webViewControllerWillDisappear: (SVWebViewController *) wvc;
- (void) webViewControllerDidDisappear: (SVWebViewController *) wvc;

@end
137 changes: 99 additions & 38 deletions SVWebViewController/SVWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,41 +79,69 @@ - (void)loadURL:(NSURL *)pageURL {

#pragma mark - View lifecycle

- (void)loadView {
self.view = self.webView;
[self loadURL:self.URL];
}
- (void) loadView {

UIView *view = [UIView.alloc initWithFrame: UIScreen.mainScreen.bounds];

view.backgroundColor = UIColor.blackColor;

self.view = view;

} // -loadView

- (void) viewDidLoad {

- (void)viewDidLoad {
[super viewDidLoad];

[self updateToolbarItems];
}

- (void)viewDidUnload {
[super viewDidUnload];
self.webView = nil;
_backBarButtonItem = nil;
_forwardBarButtonItem = nil;
_refreshBarButtonItem = nil;
_stopBarButtonItem = nil;
_actionBarButtonItem = nil;
}
} // -viewDidLoad


//- (void)viewDidUnload {
// [super viewDidUnload];
// self.webView = nil;
// _backBarButtonItem = nil;
// _forwardBarButtonItem = nil;
// _refreshBarButtonItem = nil;
// _stopBarButtonItem = nil;
// _actionBarButtonItem = nil;
//}

- (void) viewWillAppear: (BOOL) animated {

- (void)viewWillAppear:(BOOL)animated {
NSAssert(self.navigationController, @"SVWebViewController needs to be contained in a UINavigationController. If you are presenting SVWebViewController modally, use SVModalWebViewController instead.");

[super viewWillAppear:animated];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.navigationController setToolbarHidden:NO animated:animated];
[self.navigationController setToolbarHidden: NO animated: animated];
}
self.navigationController.view.backgroundColor = UIColor.clearColor;

UIWebView *wv = self.webView;

// CGRect frame = self.view.bounds;
// CGFloat height = self.navigationController.navigationBar.bounds.size.height;
//
// frame.origin.y += height;
// frame.size.height -= height;
//
wv.frame = self.view.bounds;

[self.view addSubview: wv];
[self loadURL:self.URL];

wv.scrollView.backgroundColor = UIColor.clearColor;

id<SVWebViewControllerDelegate> delegate = self.delegate;

if ([delegate respondsToSelector: @selector(webViewControllerWillAppear:)]) {

[delegate webViewControllerWillAppear: self];
}
}

} // -viewWillAppear:

- (void)viewDidAppear:(BOOL)animated {

Expand Down Expand Up @@ -161,16 +189,21 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfa
return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}


#pragma mark - Getters

- (UIWebView*)webView {

- (UIWebView *) webView {

if(!_webView) {
_webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView = [UIWebView.alloc initWithFrame: self.view.bounds];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
}
return _webView;
}

} // -webView


- (UIBarButtonItem *)backBarButtonItem {
if (!_backBarButtonItem) {
Expand Down Expand Up @@ -217,7 +250,9 @@ - (UIBarButtonItem *)actionBarButtonItem {

#pragma mark - Toolbar

- (void)updateToolbarItems {

- (void) updateToolbarItems {

self.backBarButtonItem.enabled = self.webView.canGoBack;
self.forwardBarButtonItem.enabled = self.webView.canGoForward;
self.actionBarButtonItem.enabled = !self.webViewLoads;
Expand All @@ -228,7 +263,7 @@ - (void)updateToolbarItems {
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
CGFloat toolbarWidth = 250.0f;

fixedSpace.width = 35.0f;

NSArray *items = [NSArray arrayWithObjects:
Expand All @@ -241,15 +276,10 @@ - (void)updateToolbarItems {
fixedSpace,
self.actionBarButtonItem,
nil];

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, toolbarWidth, 44.0f)];
toolbar.items = items;
toolbar.barStyle = self.navigationController.navigationBar.barStyle;
toolbar.tintColor = self.navigationController.navigationBar.tintColor;
self.navigationItem.rightBarButtonItems = items.reverseObjectEnumerator.allObjects;
}

else {

NSArray *items = [NSArray arrayWithObjects:
fixedSpace,
self.backBarButtonItem,
Expand All @@ -261,12 +291,13 @@ - (void)updateToolbarItems {
self.actionBarButtonItem,
fixedSpace,
nil];

self.navigationController.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
self.navigationController.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
// self.navigationController.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
// self.navigationController.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
self.toolbarItems = items;
}
}

} // -updateToolbarItems


#pragma mark - UIWebViewDelegate

Expand Down Expand Up @@ -347,12 +378,42 @@ - (void)stopClicked:(UIBarButtonItem *)sender {
[self updateToolbarItems];
}

- (void)actionButtonClicked:(id)sender {
NSArray *activities = @[[SVWebViewControllerActivitySafari new], [SVWebViewControllerActivityChrome new]];
- (void) actionButtonClicked: (UIBarButtonItem *) sender {

NSArray *activities = @[SVWebViewControllerActivitySafari.new,
SVWebViewControllerActivityChrome.new];

UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[self.webView.request.URL] applicationActivities:activities];
[self presentViewController:activityController animated:YES completion:nil];
}
UIActivityViewController *avc = nil;

avc = [UIActivityViewController.alloc
initWithActivityItems: @[self.webView.request.URL]
applicationActivities:activities];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

UIPopoverController *pc = nil;

pc = [UIPopoverController.alloc initWithContentViewController: avc];

pc.popoverContentSize = avc.preferredContentSize;

[pc presentPopoverFromBarButtonItem: sender
permittedArrowDirections: UIPopoverArrowDirectionAny
animated: YES];
avc.completionHandler = ^(NSString *activityType, BOOL completed) {

[pc dismissPopoverAnimated: YES];
};
}
else {

[self.navigationController presentViewController: avc
animated: YES
completion: NULL];
}

} // -actionButtonClicked:


- (void)doneButtonClicked:(id)sender {
[self.webView stopLoading];
Expand Down