我正在制作一个WKWebView应用程序,我想显示一个按钮,点击后会将我带到我网站的主页。 WebView占据了整个屏幕,当我在Main.Storyboard文件中放置一个按钮时,它没有显示出来(我认为这是因为webview重叠了它或其他东西)。
这是我的ViewController.m
//
// ViewController.m
// FLWebView
//
// Created by Steve Richey on 11/21/14.
// Copyright (c) 2014 Float Mobile Learning. Shared under an MIT license. See license.md for details.
//
#import "ViewController.h"
// Required for calls to UIWebView and WKWebView to "see" our categories
#import "UIWebView+FLUIWebView.h"
#import "WKWebView+FLWKWebView.h"
@interface ViewController ()
@end
@implementation ViewController
/*
* Called when the view has completed loading. Time to set up our WebView!
*/
- (void) viewDidLoad {
[super viewDidLoad];
// Check if WKWebView is available
// If it is present, create a WKWebView. If not, create a UIWebView.
if (NSClassFromString(@"WKWebView")) {
_webView = [[WKWebView alloc] initWithFrame: [[self view] bounds]];
} else {
_webView = [[UIWebView alloc] initWithFrame: [[self view] bounds]];
}
// Add the webView to the current view.
[[self view] addSubview: [self webView]];
// Assign this view controller as the delegate view.
// The delegate methods are below, and include methods for UIWebViewDelegate, WKNavigationDelegate, and WKUIDelegate
[[self webView] setDelegateViews: self];
// Ensure that everything will resize on device rotate.
[[self webView] setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[self view] setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
// Just to show *something* on load, we go to our favorite site.
[[self webView] loadRequestFromString:@"http://LiftedFeed.com/"];
}
/*
* Enable rotating the view when the device rotates.
*/
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
{
return YES;
}
/*
* This more or less ensures that the status bar is hidden for this view.
* We also set UIStatusBarHidden to true in the Info.plist file.
* We hide the status bar so we can use the full screen height without worrying about an offset for the status bar.
*/
- (BOOL) prefersStatusBarHidden
{
return YES;
}
#pragma mark - UIWebView Delegate Methods
/*
* Called on iOS devices that do not have WKWebView when the UIWebView requests to start loading a URL request.
* Note that it just calls shouldStartDecidePolicy, which is a shared delegate method.
* Returning YES here would allow the request to complete, returning NO would stop it.
*/
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
return [self shouldStartDecidePolicy: request];
}
/*
* Called on iOS devices that do not have WKWebView when the UIWebView starts loading a URL request.
* Note that it just calls didStartNavigation, which is a shared delegate method.
*/
- (void) webViewDidStartLoad: (UIWebView *) webView
{
[self didStartNavigation];
}
/*
* Called on iOS devices that do not have WKWebView when a URL request load failed.
* Note that it just calls failLoadOrNavigation, which is a shared delegate method.
*/
- (void) webView: (UIWebView *) webView didFailLoadWithError: (NSError *) error
{
[self failLoadOrNavigation: [webView request] withError: error];
}
/*
* Called on iOS devices that do not have WKWebView when the UIWebView finishes loading a URL request.
* Note that it just calls finishLoadOrNavigation, which is a shared delegate method.
*/
- (void) webViewDidFinishLoad: (UIWebView *) webView
{
[self finishLoadOrNavigation: [webView request]];
}
#pragma mark - WKWebView Delegate Methods
/*
* Called on iOS devices that have WKWebView when the web view wants to start navigation.
* Note that it calls shouldStartDecidePolicy, which is a shared delegate method,
* but it's essentially passing the result of that method into decisionHandler, which is a block.
*/
- (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavigationAction *) navigationAction decisionHandler: (void (^)(WKNavigationActionPolicy)) decisionHandler
{
decisionHandler([self shouldStartDecidePolicy: [navigationAction request]]);
}
/*
* Called on iOS devices that have WKWebView when the web view starts loading a URL request.
* Note that it just calls didStartNavigation, which is a shared delegate method.
*/
- (void) webView: (WKWebView *) webView didStartProvisionalNavigation: (WKNavigation *) navigation
{
[self didStartNavigation];
}
/*
* Called on iOS devices that have WKWebView when the web view fails to load a URL request.
* Note that it just calls failLoadOrNavigation, which is a shared delegate method,
* but it has to retrieve the active request from the web view as WKNavigation doesn't contain a reference to it.
*/
- (void) webView:(WKWebView *) webView didFailProvisionalNavigation: (WKNavigation *) navigation withError: (NSError *) error
{
[self failLoadOrNavigation: [webView request] withError: error];
}
/*
* Called on iOS devices that have WKWebView when the web view begins loading a URL request.
* This could call some sort of shared delegate method, but is unused currently.
*/
- (void) webView: (WKWebView *) webView didCommitNavigation: (WKNavigation *) navigation
{
// do nothing
}
/*
* Called on iOS devices that have WKWebView when the web view fails to load a URL request.
* Note that it just calls failLoadOrNavigation, which is a shared delegate method.
*/
- (void) webView: (WKWebView *) webView didFailNavigation: (WKNavigation *) navigation withError: (NSError *) error
{
[self failLoadOrNavigation: [webView request] withError: error];
}
/*
* Called on iOS devices that have WKWebView when the web view finishes loading a URL request.
* Note that it just calls finishLoadOrNavigation, which is a shared delegate method.
*/
- (void) webView: (WKWebView *) webView didFinishNavigation: (WKNavigation *) navigation
{
[self finishLoadOrNavigation: [webView request]];
}
#pragma mark - Shared Delegate Methods
/*
* This is called whenever the web view wants to navigate.
*/
- (BOOL) shouldStartDecidePolicy: (NSURLRequest *) request
{
// Determine whether or not navigation should be allowed.
// Return YES if it should, NO if not.
return YES;
}
/*
* This is called whenever the web view has started navigating.
*/
- (void) didStartNavigation
{
// Update things like loading indicators here.
}
/*
* This is called when navigation failed.
*/
- (void) failLoadOrNavigation: (NSURLRequest *) request withError: (NSError *) error
{
// Notify the user that navigation failed, provide information on the error, and so on.
}
/*
* This is called when navigation succeeds and is complete.
*/
- (void) finishLoadOrNavigation: (NSURLRequest *) request
{
// Remove the loading indicator, maybe update the navigation bar's title if you have one.
}
@end