Permalink
Browse files

Added tab bar mode. Using this mode you can show the browser as a tab…

… of a tab bar view controller. The toolbar with the navigation controls will be positioned at the top of the view automatically.
  • Loading branch information...
1 parent db8cf18 commit 1c899e97b57a4acb75b67da557e86756081ffdf6 @tonisalae committed Feb 4, 2012
View
@@ -4,7 +4,13 @@ There are already some solutions to this problem out there but non of them offer
More about me at [IndieDevStories.com](http://indiedevstories.com)
-![image](http://indiedevstories.files.wordpress.com/2012/01/tsminiwebbrowser.png)
+[![Alt][screenshot1_thumb]][screenshot1] [![Alt][screenshot2_thumb]][screenshot2] [![Alt][screenshot3_thumb]][screenshot3]
+[screenshot1_thumb]: http://dl.dropbox.com/u/7604222/GitHub/TSMiniWebBrowser/shot_01_thumb.png
+[screenshot1]: http://dl.dropbox.com/u/7604222/GitHub/TSMiniWebBrowser/shot_01.png
+[screenshot2_thumb]: http://dl.dropbox.com/u/7604222/GitHub/TSMiniWebBrowser/shot_02_thumb.png
+[screenshot2]: http://dl.dropbox.com/u/7604222/GitHub/TSMiniWebBrowser/shot_02.png
+[screenshot3_thumb]: http://dl.dropbox.com/u/7604222/GitHub/TSMiniWebBrowser/shot_03_thumb.png
+[screenshot3]: http://dl.dropbox.com/u/7604222/GitHub/TSMiniWebBrowser/shot_03.png
## Features
@@ -20,10 +26,11 @@ TSMiniWebBrowser offers the following **features**:
As you can see, there are some items that are “optional”. That means that you can configure the browser to display or not those items, depending on your app needs.
-Moreover, TSMiniWebBrowser **supports 2 presentation modes**:
+Moreover, TSMiniWebBrowser **supports 3 presentation modes**:
* **Navigation controller mode**. Using this mode you can push the browser to your navigation controller.
* **Modal mode**. Using this mode you can present the browser modally. A title bar with a dismiss button will be automatically added.
+* **Tab bar mode**. Using this mode you can show the browser as a tab of a tab bar view controller. The toolbar with the navigation controls will be positioned at the top of the view automatically.
## Usage
@@ -32,20 +39,21 @@ If you are OK with the **TSMiniWebBrowser defaults**, you can simply use this sn
TSMiniWebBrowser *webBrowser = [[TSMiniWebBrowser alloc] initWithUrl:[NSURL URLWithString:@"http://indiedevstories.com"]];
[self.navigationController pushViewController:webBrowser animated:YES];
-If you prefer, you may **customize** the browser behavior. There is also a **simple Demo app** within the project.
+If you prefer, you may **customize** the browser behavior. There is also a **simple Demo app** within the project. To test the tab bar mode go to the `application: didFinishLaunchingWithOptions:` method in `AppDelegate.m` and set the `BOOL wantTabBarDemo = NO;` value to `YES`.
TSMiniWebBrowser *webBrowser = [[TSMiniWebBrowser alloc] initWithUrl:[NSURL URLWithString:@"http://indiedevstories.com"]];
webBrowser.showURLStringOnActionSheetTitle = YES;
webBrowser.showPageTitleOnTitleBar = YES;
webBrowser.showActionButton = YES;
webBrowser.showReloadButton = YES;
- webBrowser.isModal = NO;
+ webBrowser.mode = TSMiniWebBrowserModeNavigation;
webBrowser.barStyle = UIBarStyleBlack;
- if (webBrowser.isModal) {
+ if (webBrowser.mode == TSMiniWebBrowserModeModal) {
+ webBrowser.modalDismissButtonTitle = @"Home";
[self presentModalViewController:webBrowser animated:YES];
- } else {
+ } else if(webBrowser.mode == TSMiniWebBrowserModeNavigation) {
[self.navigationController pushViewController:webBrowser animated:YES];
}
@@ -27,6 +27,13 @@
#import <UIKit/UIKit.h>
#define kToolBarHeight 44
+#define kTabBarHeight 49
+
+typedef enum {
+ TSMiniWebBrowserModeNavigation,
+ TSMiniWebBrowserModeModal,
+ TSMiniWebBrowserModeTabBar,
+} TSMiniWebBrowserMode;
@interface TSMiniWebBrowser : UIViewController <UIWebViewDelegate, UIActionSheetDelegate> {
// URL
@@ -43,11 +50,11 @@
UIBarButtonItem *buttonGoForward;
// Customization
+ TSMiniWebBrowserMode mode;
BOOL showURLStringOnActionSheetTitle;
BOOL showPageTitleOnTitleBar;
BOOL showReloadButton;
BOOL showActionButton;
- BOOL isModal;
UIBarStyle barStyle;
NSString *modalDismissButtonTitle;
NSString *forcedTitleBarText;
@@ -56,11 +63,11 @@
UIBarStyle originalBarStyle;
}
+@property (nonatomic, assign) TSMiniWebBrowserMode mode;
@property (nonatomic, assign) BOOL showURLStringOnActionSheetTitle;
@property (nonatomic, assign) BOOL showPageTitleOnTitleBar;
@property (nonatomic, assign) BOOL showReloadButton;
@property (nonatomic, assign) BOOL showActionButton;
-@property (nonatomic, assign) BOOL isModal;
@property (nonatomic, assign) UIBarStyle barStyle;
@property (nonatomic, strong) NSString *modalDismissButtonTitle;
@@ -28,20 +28,21 @@
@implementation TSMiniWebBrowser
+@synthesize mode;
@synthesize showURLStringOnActionSheetTitle;
@synthesize showPageTitleOnTitleBar;
@synthesize showReloadButton;
@synthesize showActionButton;
-@synthesize isModal;
@synthesize barStyle;
@synthesize modalDismissButtonTitle;
#pragma mark - Private Methods
-(void)setTitleBarText:(NSString*)pageTitle {
- if (isModal) {
+ if (mode == TSMiniWebBrowserModeModal) {
navigationBarModal.topItem.title = pageTitle;
- } else {
+
+ } else if(mode == TSMiniWebBrowserModeNavigation) {
if(pageTitle) [[self navigationItem] setTitle:pageTitle];
}
}
@@ -53,11 +54,13 @@ -(void) toggleBackForwardButtons {
-(void)showActivityIndicators {
[activityIndicator setHidden:NO];
+ [activityIndicator startAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
}
-(void)hideActivityIndicators {
[activityIndicator setHidden:YES];
+ [activityIndicator stopAnimating];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
@@ -84,12 +87,17 @@ -(void) initTitleBar {
}
-(void) initToolBar {
- if (!isModal) {
+ if (mode == TSMiniWebBrowserModeNavigation) {
self.navigationController.navigationBar.barStyle = barStyle;
}
CGSize viewSize = self.view.frame.size;
- toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewSize.height-kToolBarHeight, viewSize.width, kToolBarHeight)];
+ if (mode == TSMiniWebBrowserModeTabBar) {
+ toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, -1, viewSize.width, kToolBarHeight)];
+ } else {
+ toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, viewSize.height-kToolBarHeight, viewSize.width, kToolBarHeight)];
+ }
+
toolBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
toolBar.barStyle = barStyle;
[self.view addSubview:toolBar];
@@ -113,7 +121,6 @@ -(void) initToolBar {
// Activity indicator is a bit special
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
activityIndicator.frame = CGRectMake(11, 7, 20, 20);
- [activityIndicator startAnimating];
UIView *containerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 43, 33)];
[containerView addSubview:activityIndicator];
UIBarButtonItem *buttonContainer = [[UIBarButtonItem alloc] initWithCustomView:containerView];
@@ -139,10 +146,13 @@ -(void) initToolBar {
-(void) initWebView {
CGSize viewSize = self.view.frame.size;
- if (isModal) {
+ if (mode == TSMiniWebBrowserModeModal) {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, kToolBarHeight, viewSize.width, viewSize.height-kToolBarHeight*2)];
- } else {
+ } else if(mode == TSMiniWebBrowserModeNavigation) {
webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, viewSize.width, viewSize.height-kToolBarHeight)];
+ } else if(mode == TSMiniWebBrowserModeTabBar) {
+ self.view.backgroundColor = [UIColor redColor];
+ webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, kToolBarHeight-1, viewSize.width, viewSize.height-kToolBarHeight+1)];
}
webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:webView];
@@ -165,11 +175,11 @@ - (id)initWithUrl:(NSURL*)url {
urlToLoad = url;
// Defaults
+ mode = TSMiniWebBrowserModeNavigation;
showURLStringOnActionSheetTitle = YES;
showPageTitleOnTitleBar = YES;
showReloadButton = YES;
showActionButton = YES;
- isModal = NO;
modalDismissButtonTitle = NSLocalizedString(@"Done", nil);
forcedTitleBarText = nil;
}
@@ -183,29 +193,39 @@ - (void)viewDidLoad
{
[super viewDidLoad];
+ // Main view frame.
+ if (mode == TSMiniWebBrowserModeTabBar) {
+ CGFloat viewWidth = [UIScreen mainScreen].bounds.size.width;
+ CGFloat viewHeight = [UIScreen mainScreen].bounds.size.height - kTabBarHeight;
+ if (![UIApplication sharedApplication].statusBarHidden) {
+ viewHeight -= [UIApplication sharedApplication].statusBarFrame.size.height;
+ }
+ self.view.frame = CGRectMake(0, 0, viewWidth, viewHeight);
+ }
+
// Store the current navigationBar bar style to be able to restore it later.
- if (!isModal) {
+ if (mode == TSMiniWebBrowserModeNavigation) {
originalBarStyle = self.navigationController.navigationBar.barStyle;
}
- // Init web view
- [self initWebView];
-
// Init tool bar
[self initToolBar];
+ // Init web view
+ [self initWebView];
+
// Init title bar if presented modally
- if (isModal) {
+ if (mode == TSMiniWebBrowserModeModal) {
[self initTitleBar];
}
// UI state
- activityIndicator.hidden = NO;
buttonGoBack.enabled = NO;
buttonGoForward.enabled = NO;
if (forcedTitleBarText != nil) {
[self setTitleBarText:forcedTitleBarText];
}
+
}
- (void)viewDidUnload
@@ -217,7 +237,7 @@ -(void) viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
// Restore navigationBar bar style.
- if (!isModal) {
+ if (mode == TSMiniWebBrowserModeNavigation) {
self.navigationController.navigationBar.barStyle = originalBarStyle;
}
}
@@ -281,11 +301,14 @@ - (void)showActionSheet {
otherButtonTitles:NSLocalizedString(@"Open in Safari", nil), nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
- [actionSheet showInView:self.view];
- // ** Use this code instead to present the action sheet if you have a tab bar. Import AppDelegate.h
- //AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
- //[actionSheet showInView:appDelegate.tabBarController.view];
+ if (mode == TSMiniWebBrowserModeTabBar) {
+ [actionSheet showInView:self.tabBarController.view];
+
+ } else {
+ [actionSheet showInView:self.view];
+ }
+
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
@@ -337,7 +360,7 @@ - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)
[[UIApplication sharedApplication] openURL:request.URL];
return NO;
}
-
+
if ([[request.URL absoluteString] hasPrefix:@"http://www.youtube.com/v/"] ||
[[request.URL absoluteString] hasPrefix:@"http://itunes.apple.com/"] ||
[[request.URL absoluteString] hasPrefix:@"http://phobos.apple.com/"]) {
@@ -36,4 +36,6 @@
@property (strong, nonatomic) UINavigationController *navigationController;
+@property (strong, nonatomic) UITabBarController *tabBarController;
+
@end
@@ -27,20 +27,44 @@
#import "AppDelegate.h"
#import "ViewController.h"
+#import "TSMiniWebBrowser.h"
@implementation AppDelegate
@synthesize window = _window;
@synthesize viewController = _viewController;
@synthesize navigationController = _navigationController;
+@synthesize tabBarController = _tabBarController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
+ BOOL wantTabBarDemo = NO;
+
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- // Override point for customization after application launch.
- self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
- self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
- self.window.rootViewController = self.navigationController;
+
+ // Tabbar mode demo init code
+ if (wantTabBarDemo) {
+ UIViewController *dummyViewController = [[UIViewController alloc] init];
+
+ TSMiniWebBrowser *webBrowser = [[TSMiniWebBrowser alloc] initWithUrl:[NSURL URLWithString:@"http://indiedevstories.com"]];
+ webBrowser.barStyle = UIBarStyleBlack;
+ webBrowser.mode = TSMiniWebBrowserModeTabBar;
+
+ self.tabBarController = [[UITabBarController alloc] init];
+ self.tabBarController.viewControllers = [NSArray arrayWithObjects:webBrowser, dummyViewController, nil];
+
+ self.window.rootViewController = self.tabBarController;
+
+ // navigation and modal mode demo init code
+ } else {
+ self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
+
+ self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
+
+ self.window.rootViewController = self.navigationController;
+ }
+
+
[self.window makeKeyAndVisible];
return YES;
}
@@ -78,19 +78,19 @@ - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interface
- (IBAction)buttonTouchUp:(id)sender {
TSMiniWebBrowser *webBrowser = [[TSMiniWebBrowser alloc] initWithUrl:[NSURL URLWithString:@"http://indiedevstories.com"]];
- /*webBrowser.showURLStringOnActionSheetTitle = YES;
- webBrowser.showPageTitleOnTitleBar = YES;
- webBrowser.showActionButton = YES;
- webBrowser.showReloadButton = YES;*/
- //[webBrowser setFixedTitleBarText:@"Test Title Text"];
- webBrowser.isModal = NO;
+// webBrowser.showURLStringOnActionSheetTitle = YES;
+// webBrowser.showPageTitleOnTitleBar = YES;
+// webBrowser.showActionButton = YES;
+// webBrowser.showReloadButton = YES;
+// [webBrowser setFixedTitleBarText:@"Test Title Text"]; // This has priority over "showPageTitleOnTitleBar".
+ webBrowser.mode = TSMiniWebBrowserModeNavigation;
webBrowser.barStyle = UIBarStyleBlack;
- if (webBrowser.isModal) {
+ if (webBrowser.mode == TSMiniWebBrowserModeModal) {
webBrowser.modalDismissButtonTitle = @"Home";
[self presentModalViewController:webBrowser animated:YES];
- } else {
+ } else if(webBrowser.mode == TSMiniWebBrowserModeNavigation) {
[self.navigationController pushViewController:webBrowser animated:YES];
}
}

0 comments on commit 1c899e9

Please sign in to comment.