Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New 'scalesPageToFit' parameter in init methods #137

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions SVWebViewController/SVModalWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@
@interface SVModalWebViewController : UINavigationController

- (instancetype)initWithAddress:(NSString*)urlString;
- (instancetype)initWithAddress:(NSString*)urlString scalesPageToFit:(BOOL)scalesPageToFit;
- (instancetype)initWithURL:(NSURL *)URL;
- (instancetype)initWithURL:(NSURL *)URL scalesPageToFit:(BOOL)scalesPageToFit;
- (instancetype)initWithURLRequest:(NSURLRequest *)request;
- (instancetype)initWithURLRequest:(NSURLRequest *)request scalesPageToFit:(BOOL)scalesPageToFit;

@property (nonatomic, strong) UIColor *barsTintColor;
@property (nonatomic, weak) id<UIWebViewDelegate> webViewDelegate;
Expand Down
18 changes: 15 additions & 3 deletions SVWebViewController/SVModalWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,29 @@ - (instancetype)initWithAddress:(NSString*)urlString {
return [self initWithURL:[NSURL URLWithString:urlString]];
}

- (instancetype)initWithAddress:(NSString*)urlString scalesPageToFit:(BOOL)scalesPageToFit {
return [self initWithURL:[NSURL URLWithString:urlString] scalesPageToFit:scalesPageToFit];
}

- (instancetype)initWithURL:(NSURL *)URL {
return [self initWithURLRequest:[NSURLRequest requestWithURL:URL]];
}

- (instancetype)initWithURL:(NSURL *)URL scalesPageToFit:(BOOL)scalesPageToFit {
return [self initWithURLRequest:[NSURLRequest requestWithURL:URL] scalesPageToFit:scalesPageToFit];
}

- (instancetype)initWithURLRequest:(NSURLRequest *)request {
self.webViewController = [[SVWebViewController alloc] initWithURLRequest:request];
return [self initWithURLRequest:request scalesPageToFit:YES];
}

- (instancetype)initWithURLRequest:(NSURLRequest *)request scalesPageToFit:(BOOL)scalesPageToFit {
self.webViewController = [[SVWebViewController alloc] initWithURLRequest:request scalesPageToFit:scalesPageToFit];
if (self = [super initWithRootViewController:self.webViewController]) {
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self.webViewController
action:@selector(doneButtonTapped:)];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
self.webViewController.navigationItem.leftBarButtonItem = doneButton;
else
Expand All @@ -52,7 +64,7 @@ - (instancetype)initWithURLRequest:(NSURLRequest *)request {

- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:NO];

self.webViewController.title = self.title;
self.navigationBar.tintColor = self.barsTintColor;
}
Expand Down
3 changes: 3 additions & 0 deletions SVWebViewController/SVWebViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@
@interface SVWebViewController : UIViewController

- (instancetype)initWithAddress:(NSString*)urlString;
- (instancetype)initWithAddress:(NSString*)urlString scalesPageToFit:(BOOL)scalesPageToFit;
- (instancetype)initWithURL:(NSURL*)URL;
- (instancetype)initWithURL:(NSURL*)URL scalesPageToFit:(BOOL)scalesPageToFit;
- (instancetype)initWithURLRequest:(NSURLRequest *)request;
- (instancetype)initWithURLRequest:(NSURLRequest*)request scalesPageToFit:(BOOL)scalesPageToFit;

@property (nonatomic, weak) id<UIWebViewDelegate> delegate;

Expand Down
50 changes: 32 additions & 18 deletions SVWebViewController/SVWebViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ @interface SVWebViewController () <UIWebViewDelegate>

@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSURLRequest *request;
@property (nonatomic, assign) BOOL scalesPageToFit;

@end

Expand All @@ -36,17 +37,30 @@ - (void)dealloc {
}

- (instancetype)initWithAddress:(NSString *)urlString {
return [self initWithURL:[NSURL URLWithString:urlString]];
return [self initWithURL:[NSURL URLWithString:urlString] scalesPageToFit:YES];
}

- (instancetype)initWithAddress:(NSString *)urlString scalesPageToFit:(BOOL)scalesPageToFit {
return [self initWithURL:[NSURL URLWithString:urlString] scalesPageToFit:scalesPageToFit];
}

- (instancetype)initWithURL:(NSURL*)pageURL {
return [self initWithURLRequest:[NSURLRequest requestWithURL:pageURL]];
return [self initWithURLRequest:[NSURLRequest requestWithURL:pageURL] scalesPageToFit:YES];
}

- (instancetype)initWithURL:(NSURL*)pageURL scalesPageToFit:(BOOL)scalesPageToFit {
return [self initWithURLRequest:[NSURLRequest requestWithURL:pageURL] scalesPageToFit:scalesPageToFit];
}

- (instancetype)initWithURLRequest:(NSURLRequest*)request {
return [self initWithURLRequest:request scalesPageToFit:YES];
}

- (instancetype)initWithURLRequest:(NSURLRequest*)request scalesPageToFit:(BOOL)scalesPageToFit {
self = [super init];
if (self) {
self.request = request;
self.scalesPageToFit = scalesPageToFit;
}
return self;
}
Expand Down Expand Up @@ -92,7 +106,7 @@ - (void)viewWillAppear:(BOOL)animated {

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
[self.navigationController setToolbarHidden:YES animated:animated];
}
Expand All @@ -106,7 +120,7 @@ - (void)viewDidDisappear:(BOOL)animated {
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
return YES;

return toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown;
}

Expand All @@ -116,7 +130,7 @@ - (UIWebView*)webView {
if(!_webView) {
_webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
_webView.delegate = self;
_webView.scalesPageToFit = YES;
_webView.scalesPageToFit = self.scalesPageToFit;
}
return _webView;
}
Expand Down Expand Up @@ -169,16 +183,16 @@ - (UIBarButtonItem *)actionBarButtonItem {
- (void)updateToolbarItems {
self.backBarButtonItem.enabled = self.self.webView.canGoBack;
self.forwardBarButtonItem.enabled = self.self.webView.canGoForward;

UIBarButtonItem *refreshStopBarButtonItem = self.self.webView.isLoading ? self.stopBarButtonItem : self.refreshBarButtonItem;

UIBarButtonItem *fixedSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
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:
fixedSpace,
refreshStopBarButtonItem,
Expand All @@ -189,14 +203,14 @@ - (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,
Expand All @@ -209,7 +223,7 @@ - (void)updateToolbarItems {
self.actionBarButtonItem,
fixedSpace,
nil];

self.navigationController.toolbar.barStyle = self.navigationController.navigationBar.barStyle;
self.navigationController.toolbar.tintColor = self.navigationController.navigationBar.tintColor;
self.toolbarItems = items;
Expand All @@ -234,7 +248,7 @@ - (void)webViewDidFinishLoad:(UIWebView *)webView {
if (self.navigationItem.title == nil) {
self.navigationItem.title = [webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

[self updateToolbarItems];

if ([self.delegate respondsToSelector:@selector(webViewDidFinishLoad:)]) {
Expand Down Expand Up @@ -282,13 +296,13 @@ - (void)actionButtonTapped:(id)sender {
NSURL *url = self.webView.request.URL ? self.webView.request.URL : self.request.URL;
if (url != nil) {
NSArray *activities = @[[SVWebViewControllerActivitySafari new], [SVWebViewControllerActivityChrome new]];
if ([[url absoluteString] hasPrefix:@"file:///"]) {

if (url.fileURL) {
UIDocumentInteractionController *dc = [UIDocumentInteractionController interactionControllerWithURL:url];
[dc presentOptionsMenuFromRect:self.view.bounds inView:self.view animated:YES];
} else {
UIActivityViewController *activityController = [[UIActivityViewController alloc] initWithActivityItems:@[url] applicationActivities:activities];

#ifdef __IPHONE_8_0
if (floor(NSFoundationVersionNumber) > NSFoundationVersionNumber_iOS_7_1 &&
UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
Expand All @@ -298,13 +312,13 @@ - (void)actionButtonTapped:(id)sender {
ctrl.barButtonItem = sender;
}
#endif

[self presentViewController:activityController animated:YES completion:nil];
}
}
}

- (void)doneButtonTapped:(id)sùender {
- (void)doneButtonTapped:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}

Expand Down