WKWebView in Interface Builder

前端 未结 12 837
南旧
南旧 2020-11-30 01:51

It seems that the IB object templates in XCode 6 beta are still creating old-style objects (UIWebView for iOS and WebView for OSX). Hopefully Apple will update them for the

相关标签:
12条回答
  • 2020-11-30 02:32

    Here's a simple Swift 3 version based on crx_au's excellent answer.

    import WebKit
    
    class WKWebView_IBWrapper: WKWebView {
        required convenience init?(coder: NSCoder) {
            let config = WKWebViewConfiguration()
            //config.suppressesIncrementalRendering = true //any custom config you want to add
            self.init(frame: .zero, configuration: config)
            self.translatesAutoresizingMaskIntoConstraints = false
        }
    }
    

    Create a UIView in Interface Builder, assign your constraints, and assign it WKWebView_IBWrapper as a custom class, like so:

    Utilities -> Identity Inspector Tab[1]

    0 讨论(0)
  • 2020-11-30 02:35

    If you still face this issue in recent versions of Xcode, i.e. v9.2+, simply import Webkit to your ViewController:

    #import <WebKit/WebKit.h>
    
    1. Before the fix:

    2. After the fix:

    0 讨论(0)
  • 2020-11-30 02:39

    As pointed out by some, as of Xcode 6.4, WKWebView is still not available on Interface Builder. However, it is very easy to add them via code.

    I'm just using this in my ViewController. Skipping Interface builder

    import UIKit
    import WebKit
    
    class ViewController: UIViewController {
    
        private var webView: WKWebView?
    
        override func loadView() {
            webView = WKWebView()
    
            //If you want to implement the delegate
            //webView?.navigationDelegate = self
    
            view = webView
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            if let url = URL(string: "https://google.com") {
                let req = URLRequest(url: url)
                webView?.load(req)
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:39

    This is now apparently fixed in Xcode 9b4. The release notes say "WKWebView is available in the iOS object library."

    I haven't looked deeper to see if it requires iOS 11 or is backward compatible yet.

    0 讨论(0)
  • 2020-11-30 02:39

    You can instantiate and configure a WKWebView in IB since Xcode 9, no need to do it in code.

    Note that your deployment target has to be higher than iOS 10 though or you will get a compile-time error.

    0 讨论(0)
  • 2020-11-30 02:44

    With Xcode 8 this is now possible, but the means of achieving it is a little hacky to say the least. But hey, a working solution is a working solution, right? Let me explain.

    WKWebView's initWithCoder: is no longer annotated as "NS_UNAVAILABLE". It now looks as shown below.

    - (nullable instancetype)initWithCoder:(NSCoder *)coder NS_DESIGNATED_INITIALIZER;
    

    Start by subclassing WKWebView and override initWithCoder. Instead of calling super initWithCoder, you'll need to use a different init method, such as initWithFrame:configuration:. Quick example below.

    - (instancetype)initWithCoder:(NSCoder *)coder
    {
        // An initial frame for initialization must be set, but it will be overridden 
        // below by the autolayout constraints set in interface builder. 
        CGRect frame = [[UIScreen mainScreen] bounds];
        WKWebViewConfiguration *myConfiguration = [WKWebViewConfiguration new];
    
        // Set any configuration parameters here, e.g.
        // myConfiguration.dataDetectorTypes = WKDataDetectorTypeAll; 
    
        self = [super initWithFrame:frame configuration:myConfiguration];
    
        // Apply constraints from interface builder.
        self.translatesAutoresizingMaskIntoConstraints = NO;
    
        return self;
    }
    

    Over in your Storyboard, use a UIView and give it a custom class of your new subclass. The rest is business as usual (setting auto-layout constraints, linking the view to an outlet in a controller, etc).

    Finally, WKWebView scales content differently to UIWebView. Many people are likely going to want to follow the simple advice in Suppress WKWebView from scaling content to render at same magnification as UIWebView does to make WKWebView more closely follow the UIWebView behaviour in this regard.

    0 讨论(0)
提交回复
热议问题