Silver Challenge Answer


#1

Hey there,

I solved the silver challenge in an app of my own, the UIToolbar is a little ugly but it gets the job done.

[code]#import <UIKit/UIKit.h>

@interface AFASiteViewController : UIViewController

@property (strong, nonatomic) NSURL* url;
@property (nonatomic, strong)UIToolbar *tBar;

@end[/code]

[code]#import “AFASiteViewController.h”

@interface AFASiteViewController ()

@property (nonatomic) UIWebView* siteView;

@end

@implementation AFASiteViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    self.tabBarItem.title = @“Website”;
    _url = [NSURL URLWithString:@“http://duncantrussell.com/”];

    }
    return self;
    }

-(void)configureToolbar
{

UIToolbar *toolbar=[[UIToolbar alloc]initWithFrame:CGRectMake(0,0, 350,50)];


UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pageBack)];


UIBarButtonItem *forwardButton=[[UIBarButtonItem alloc]initWithTitle:@"Forward" style:UIBarButtonItemStyleBordered target:self action:@selector(pageForward)];

NSArray *toolbarItems=@[backButton, forwardButton];

[toolbar setItems:toolbarItems animated:NO];

_tBar = toolbar;

[_siteView addSubview:toolbar];

}

  • (void)pageBack
    {
    if (_siteView.canGoBack)
    {
    [_siteView goBack];
    }

}

  • (void)pageForward
    {
    if (_siteView.canGoForward)
    {
    [_siteView goForward];
    }

}

  • (void) loadView
    {
    _siteView = [[UIWebView alloc] init];
    self.view = _siteView;
    NSURLRequest *request = [NSURLRequest requestWithURL:_url];
    [self.siteView loadRequest:request];
    _siteView.scalesPageToFit = YES;
    [self configureToolbar];

}

  • (void)viewDidLoad
    {
    [super viewDidLoad];

}

  • (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@end[/code]

The issue that I’m currently struggling with is how to have back and forward buttons without blocking the content behind their bar. This would be something like relative positioning in HTML (where the object is in normal document flow) versus absolute position (where the object will position over everything else).

I’m wondering if it’s a good idea to scrap the UIToolbar and go for a UINavigationbar, but the text book seems to think the UIToolbar is the way to go. The docs https://developer.apple.com/library/ios/qa/qa1797/_index.html say that the way to solve this is with a visual constraints either with a xib or programmatically (the latter of which seems like a bitch) or a scroll view, so I’m going to try both and post back. If any of you guys have solved this I’d love to see!


#2
@property (nonatomic, strong) UIScrollView *scrollView;
- (void) configureScrollView
{
    CGRect screenRect = CGRectMake(0, 50, 480, 1000);
    _scrollView = [[UIScrollView alloc] initWithFrame:screenRect];
    _scrollView.contentSize = CGSizeMake(320, 800);
    _scrollView.scrollEnabled = YES;
    
}


- (void) loadView
{
    _siteView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 50, 320, 800)];
    _siteView.scalesPageToFit = YES;
    [self configureScrollView];
    self.view = _scrollView;
     [_scrollView addSubview:_siteView];
    NSURLRequest *request = [NSURLRequest requestWithURL:_url];
    [self.siteView loadRequest:request];
    [self configureToolbar];
    
    
    
}

#3

All code posted from BNRWebViewController.m[code]#import “BNRWebViewController.h”

@interface BNRWebViewController()

@property (nonatomic) UIWebView *webView;
@property (nonatomic) UIBarButtonItem *back;
@property (nonatomic) UIBarButtonItem *forward;

@end

@implementation BNRWebViewController

-(void)webViewDidFinishLoad:(UIWebView *)webView {
NSLog(@“finished loading”);
[self.back setEnabled:[self.webView canGoBack]]; //these two do what you think they would do
[self.forward setEnabled:[self.webView canGoForward]];
}

-(void)loadView {
self.webView = [[UIWebView alloc] init];
self.webView.scalesPageToFit = YES;
UINavigationItem *navItem = self.navigationItem;
self.back = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRewind
target:self
action:@selector(goBack:)];
self.forward = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward
target:self
action:@selector(goForward:)];
navItem.rightBarButtonItems = @[self.forward, self.back];
self.view = self.webView;
[self.back setEnabled:NO]; //these two set to NO so you can’t click buttons when the first page loads
[self.forward setEnabled:NO];
self.webView.delegate = self;
}

-(void)goBack: (id) sender {
[self.webView goBack];
}

-(void)goForward: (id) sender {
[self.webView goForward];
}

-(void)setURL:(NSURL *)URL {
_URL = URL;
if (self.URL) {
NSURLRequest req = [NSURLRequest requestWithURL:self.URL];
[(UIWebView
)self.view loadRequest:req];
}
}

@end[/code]