Refactoring original view to remove both toolbars. Moved back button and send to button to top.

This commit is contained in:
Samuel Clay 2014-01-08 13:38:18 -08:00
parent b9271f6aab
commit b2ccb06177
4 changed files with 64 additions and 253 deletions

View file

@ -11,53 +11,24 @@
@class NewsBlurAppDelegate;
static const CGFloat kNavBarHeight = 78.0f;
static const CGFloat kLabelHeight = 18.0f;
static const CGFloat kMargin = 6.0f;
static const CGFloat kSpacer = 2.0f;
static const CGFloat kLabelFontSize = 12.0f;
static const CGFloat kAddressHeight = 30.0f;
static const CGFloat kButtonWidth = 68.0f;
@interface OriginalStoryViewController : BaseViewController
<UIActionSheetDelegate, UITextFieldDelegate, UIWebViewDelegate> {
NewsBlurAppDelegate *appDelegate;
IBOutlet UIBarButtonItem * closeButton;
NSString *activeUrl;
NSMutableArray *visitedUrls;
UIWebView *webView;
UIBarButtonItem* back;
UIBarButtonItem* forward;
UIBarButtonItem* refresh;
UIBarButtonItem* pageAction;
UILabel *pageTitle;
UITextField *pageUrl;
UIToolbar *toolbar;
UIBarButtonItem *backBarButton;
}
@property (nonatomic) IBOutlet NewsBlurAppDelegate *appDelegate;
@property (nonatomic) IBOutlet UINavigationBar *navBar;
@property (nonatomic) IBOutlet UIBarButtonItem *closeButton;
@property (nonatomic) IBOutlet UIWebView *webView;
@property (nonatomic) IBOutlet UIBarButtonItem* back;
@property (nonatomic) IBOutlet UIBarButtonItem* forward;
@property (nonatomic) IBOutlet UIBarButtonItem* refresh;
@property (nonatomic) IBOutlet UIBarButtonItem* pageAction;
@property (nonatomic) IBOutlet UILabel *pageTitle;
@property (nonatomic) IBOutlet UITextField *pageUrl;
@property (nonatomic) IBOutlet UIToolbar *toolbar;
- (void)layoutNavBar;
- (void)layoutForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation;
- (IBAction) doCloseOriginalStoryViewController;
- (IBAction) doOpenActionSheet:(id)sender;
- (IBAction)loadAddress:(id)sender;
- (IBAction)webViewGoBack:(id)sender;
- (IBAction)webViewGoForward:(id)sender;
- (IBAction)webViewRefresh:(id)sender;
- (void)updateTitle:(UIWebView*)aWebView;
- (void)updateAddress:(NSURLRequest*)request;
- (void)updateButtons;
@end

View file

@ -18,16 +18,7 @@
@implementation OriginalStoryViewController
@synthesize appDelegate;
@synthesize closeButton;
@synthesize webView;
@synthesize back;
@synthesize forward;
@synthesize refresh;
@synthesize pageAction;
@synthesize pageTitle;
@synthesize pageUrl;
@synthesize toolbar;
@synthesize navBar;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
@ -38,24 +29,43 @@
- (void)viewWillAppear:(BOOL)animated {
// NSLog(@"Original Story View: %@", [appDelegate activeOriginalStoryURL]);
appDelegate.originalStoryViewNavController.navigationBar.hidden = YES;
NSURLRequest *request = [[NSURLRequest alloc] initWithURL:appDelegate.activeOriginalStoryURL] ;
[self updateAddress:request];
[self.pageTitle setText:[[appDelegate activeStory] objectForKey:@"story_title"]];
self.navigationItem.title = [[appDelegate activeStory] objectForKey:@"story_title"];
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
[self layoutForInterfaceOrientation:orientation];
UIImage *separatorImage = [UIImage imageNamed:@"bar-separator.png"];
UIBarButtonItem *separatorBarButton = [UIBarButtonItem barItemWithImage:separatorImage
target:nil
action:nil];
[separatorBarButton setEnabled:NO];
UIBarButtonItem *sendToBarButton = [UIBarButtonItem
barItemWithImage:[UIImage imageNamed:@"barbutton_sendto.png"]
target:self
action:@selector(doOpenActionSheet:)];
backBarButton = [UIBarButtonItem
barItemWithImage:[UIImage imageNamed:@"barbutton_back.png"]
target:self
action:@selector(webViewGoBack:)];
backBarButton.enabled = NO;
self.navigationItem.rightBarButtonItems = @[sendToBarButton,
separatorBarButton,
backBarButton
];
appDelegate.originalStoryViewNavController.navigationBar.hidden = YES;
activeUrl = [appDelegate.activeOriginalStoryURL absoluteString];
[self loadAddress:nil];
self.navigationItem.title = [[appDelegate activeStory] objectForKey:@"story_title"];
[MBProgressHUD hideHUDForView:self.webView animated:YES];
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.webView animated:YES];
HUD.labelText = @"On its way...";
[HUD hide:YES afterDelay:2];
}
- (void)viewDidAppear:(BOOL)animated {
[self layoutNavBar];
}
- (void)viewWillDisappear:(BOOL)animated {
@ -72,102 +82,8 @@
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
[self layoutForInterfaceOrientation:toInterfaceOrientation];
}
- (void)viewDidLoad {
CGRect labelFrame = CGRectMake(kMargin, kSpacer + 20,
navBar.bounds.size.width - 2*kMargin,
kLabelHeight);
UILabel *label = [[UILabel alloc] initWithFrame:labelFrame];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:12];
label.textColor = UIColorFromRGB(0x404040);
label.shadowColor = UIColorFromRGB(0xFAFAFA);
label.shadowOffset = CGSizeMake(0.0f, -1.0f);
label.textAlignment = NSTextAlignmentCenter;
label.text = [[appDelegate activeStory] objectForKey:@"story_title"];
[navBar addSubview:label];
self.pageTitle = label;
self.navigationItem.title = [[appDelegate activeStory] objectForKey:@"story_title"];
UIBarButtonItem *close = [[UIBarButtonItem alloc]
initWithTitle:@"Close"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(doCloseOriginalStoryViewController)];
close.width = kButtonWidth;
CGRect closeButtonFrame = CGRectMake(-20,
kSpacer*2.0 + kLabelHeight - 7.0f + 20,
kButtonWidth + kMargin,
44.0);
TransparentToolbar* tools = [[TransparentToolbar alloc]
initWithFrame:closeButtonFrame];
[tools setItems:[NSArray arrayWithObject:close] animated:NO];
[tools setTintColor:UIColorFromRGB(0x183353)];
[navBar addSubview:tools];
CGRect addressFrame = CGRectMake(closeButtonFrame.origin.x +
closeButtonFrame.size.width +
kMargin,
kSpacer*2.0 + kLabelHeight + 20,
labelFrame.size.width
- kButtonWidth - kMargin*2 + 20,
kAddressHeight);
UITextField *address = [[UITextField alloc] initWithFrame:addressFrame];
address.autoresizingMask = UIViewAutoresizingFlexibleWidth;
address.borderStyle = UITextBorderStyleRoundedRect;
address.font = [UIFont systemFontOfSize:14];
address.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
address.adjustsFontSizeToFitWidth = NO;
address.keyboardType = UIKeyboardTypeURL;
address.autocapitalizationType = UITextAutocapitalizationTypeNone;
address.clearButtonMode = UITextFieldViewModeWhileEditing;
address.enablesReturnKeyAutomatically = YES;
address.returnKeyType = UIReturnKeyGo;
address.delegate = self;
[navBar addSubview:address];
self.pageUrl = address;
UIImage *backImage = [UIImage imageNamed:@"barbutton_back.png"];
NBBarButtonItem *backButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
backButton.bounds = CGRectMake(0, 0, 44, 44);
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:@selector(webViewGoBack:) forControlEvents:UIControlEventTouchUpInside];
[back setCustomView:backButton];
UIImage *forwardImage = [UIImage imageNamed:@"barbutton_forward.png"];
NBBarButtonItem *forwardButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
forwardButton.bounds = CGRectMake(0, 0, 44, 44);
[forwardButton setImage:forwardImage forState:UIControlStateNormal];
[forwardButton addTarget:self action:@selector(webViewGoForward:) forControlEvents:UIControlEventTouchUpInside];
[forward setCustomView:forwardButton];
UIImage *refreshImage = [UIImage imageNamed:@"barbutton_refresh.png"];
NBBarButtonItem *refreshButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
refreshButton.bounds = CGRectMake(0, 0, 44, 44);
[refreshButton setImage:refreshImage forState:UIControlStateNormal];
[refreshButton addTarget:self action:@selector(webViewRefresh:) forControlEvents:UIControlEventTouchUpInside];
[refresh setCustomView:refreshButton];
UIImage *sendtoImage = [UIImage imageNamed:@"barbutton_sendto.png"];
NBBarButtonItem *sendtoButton = [NBBarButtonItem buttonWithType:UIButtonTypeCustom];
sendtoButton.bounds = CGRectMake(0, 0, 44, 44);
[sendtoButton setImage:sendtoImage forState:UIControlStateNormal];
[sendtoButton addTarget:self action:@selector(doOpenActionSheet:) forControlEvents:UIControlEventTouchUpInside];
[pageAction setCustomView:sendtoButton];
}
- (void)layoutNavBar {
CGRect navBarFrame = self.view.bounds;
navBarFrame.size.height = kNavBarHeight;
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
navBar.frame = navBarFrame;
navBar.translucent = NO;
toolbar.translucent = NO;
// self.navigationItem.title = [[appDelegate activeStory] objectForKey:@"story_title"];
}
- (IBAction)webViewGoBack:(id)sender {
@ -182,87 +98,46 @@
[webView reload];
}
- (void) layoutForInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
[self layoutNavBar];
CGSize toolbarSize = [self.toolbar sizeThatFits:self.view.bounds.size];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
self.toolbar.frame = CGRectMake(-10.0f,
CGRectGetHeight(self.view.bounds) - toolbarSize.height,
toolbarSize.width + 20, toolbarSize.height);
} else {
self.toolbar.frame = (CGRect){CGPointMake(0.f, CGRectGetHeight(self.view.bounds) -
toolbarSize.height), toolbarSize};
self.webView.frame = (CGRect){CGPointMake(0, 0), CGSizeMake(CGRectGetWidth(self.view.bounds), CGRectGetMinY(self.toolbar.frame))};
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[self loadAddress:nil];
return YES;
}
- (IBAction)loadAddress:(id)sender {
NSString* urlString = self.pageUrl.text;
NSURL* url = [NSURL URLWithString:urlString];
if (!url.scheme) {
NSString* modifiedURLString = [NSString stringWithFormat:@"%@", urlString];
url = [NSURL URLWithString:modifiedURLString];
}
if ([self.webView isLoading]) {
[self.webView stopLoading];
}
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
[self.pageUrl resignFirstResponder];
[self.pageTitle setText:@"Loading..."];
self.navigationItem.title = @"Loading...";
}
# pragma mark: -
# pragma mark: UIWebViewDelegate protocol
- (BOOL)webView:(UIWebView *)webView
- (BOOL)webView:(UIWebView *)aWebView
shouldStartLoadWithRequest:(NSURLRequest *)request
navigationType:(UIWebViewNavigationType)navigationType {
if ([aWebView canGoBack]) {
[backBarButton setEnabled:YES];
} else {
[backBarButton setEnabled:NO];
}
if ([[[request URL] scheme] isEqual:@"mailto"]) {
[[UIApplication sharedApplication] openURL:[request URL]];
return NO;
} else if (navigationType == UIWebViewNavigationTypeLinkClicked) {
[self updateAddress:request];
activeUrl = [[request URL] absoluteString];
[self loadAddress:nil];
return NO;
}
NSURL* mainUrl = [request mainDocumentURL];
NSString* absoluteString = [mainUrl absoluteString];
self.pageUrl.text = absoluteString;
return YES;
}
- (void)webViewDidStartLoad:(UIWebView *)aWebView
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
[self updateButtons];
}
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
[MBProgressHUD hideHUDForView:self.webView animated:YES];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self updateButtons];
[self updateTitle:aWebView];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
[self updateButtons];
// User clicking on another link before the page loads is OK.
if ([error code] != NSURLErrorCancelled) {
@ -273,21 +148,24 @@
- (void)updateTitle:(UIWebView*)aWebView
{
NSString *pageTitleValue = [aWebView stringByEvaluatingJavaScriptFromString:@"document.title"];
self.pageTitle.text = [pageTitleValue stringByDecodingHTMLEntities];
self.navigationItem.title = [pageTitleValue stringByDecodingHTMLEntities];
}
- (void)updateAddress:(NSURLRequest*)request
{
NSURL *url = [request URL];
self.pageUrl.text = [url absoluteString];
[self loadAddress:nil];
}
- (void)updateButtons
{
self.forward.enabled = self.webView.canGoForward;
self.back.enabled = self.webView.canGoBack;
- (IBAction)loadAddress:(id)sender {
NSString* urlString = activeUrl;
NSURL* url = [NSURL URLWithString:urlString];
if (!url.scheme) {
NSString* modifiedURLString = [NSString stringWithFormat:@"%@", urlString];
url = [NSURL URLWithString:modifiedURLString];
}
if ([self.webView isLoading]) {
[self.webView stopLoading];
}
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:request];
self.navigationItem.title = @"Loading...";
}
- (void)didReceiveMemoryWarning {
@ -297,15 +175,9 @@
// Release any cached data, images, etc that aren't in use.
}
- (IBAction)doCloseOriginalStoryViewController {
// NSLog(@"Close Original Story: %@", appDelegate);
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)doOpenActionSheet:(id)sender {
// NSURL *url = [NSURL URLWithString:appDelegate.activeOriginalStoryURL];
NSURL *url = [NSURL URLWithString:self.pageUrl.text];
NSURL *url = [NSURL URLWithString:self.webView.request.URL.absoluteString];
NSString *title = [[webView stringByEvaluatingJavaScriptFromString:@"document.title"]
stringByDecodingHTMLEntities];

View file

@ -244,6 +244,12 @@
UIInterfaceOrientation orientation = [UIApplication sharedApplication].statusBarOrientation;
[self layoutForInterfaceOrientation:orientation];
[self adjustDragBar:orientation];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
initWithTitle:@" "
style:UIBarButtonItemStylePlain
target:nil action:nil];
}
- (void)viewDidAppear:(BOOL)animated {

View file

@ -7,11 +7,6 @@
<objects>
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="OriginalStoryViewController">
<connections>
<outlet property="back" destination="6" id="26"/>
<outlet property="forward" destination="14" id="23"/>
<outlet property="pageAction" destination="17" id="22"/>
<outlet property="refresh" destination="52" id="60"/>
<outlet property="toolbar" destination="5" id="45"/>
<outlet property="view" destination="1" id="39"/>
<outlet property="webView" destination="4" id="13"/>
</connections>
@ -22,7 +17,7 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<webView multipleTouchEnabled="YES" contentMode="scaleToFill" scalesPageToFit="YES" id="4">
<rect key="frame" x="0.0" y="0.0" width="320" height="436"/>
<rect key="frame" x="0.0" y="0.0" width="320" height="480"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" cocoaTouchSystemColor="scrollViewTexturedBackgroundColor"/>
<dataDetectorType key="dataDetectorTypes" phoneNumber="YES" link="YES" address="YES" calendarEvent="YES"/>
@ -30,42 +25,9 @@
<outlet property="delegate" destination="-1" id="7"/>
</connections>
</webView>
<toolbar clipsSubviews="YES" contentMode="scaleToFill" id="5">
<rect key="frame" x="0.0" y="436" width="320" height="44"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" flexibleMinY="YES"/>
<items>
<barButtonItem image="barbutton_back.png" width="44" style="plain" id="6">
<connections>
<action selector="goBack" destination="4" id="27"/>
</connections>
</barButtonItem>
<barButtonItem image="barbutton_forward.png" width="44" style="plain" id="14">
<connections>
<action selector="goForward" destination="4" id="28"/>
</connections>
</barButtonItem>
<barButtonItem style="plain" systemItem="flexibleSpace" id="44"/>
<barButtonItem image="barbutton_refresh.png" width="44" style="plain" id="52">
<connections>
<action selector="reload" destination="4" id="53"/>
</connections>
</barButtonItem>
<barButtonItem image="barbutton_sendto.png" width="44" style="plain" id="17">
<connections>
<action selector="doOpenActionSheet:" destination="-1" id="61"/>
</connections>
</barButtonItem>
</items>
</toolbar>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<simulatedStatusBarMetrics key="simulatedStatusBarMetrics"/>
</view>
</objects>
<resources>
<image name="barbutton_back.png" width="24" height="24"/>
<image name="barbutton_forward.png" width="24" height="24"/>
<image name="barbutton_refresh.png" width="16" height="18"/>
<image name="barbutton_sendto.png" width="20" height="15"/>
</resources>
</document>