以编程方式在具有自动布局的UIView内部使用WKwebView

时间:2015-08-17 18:03:34

标签: uiview autolayout constraints wkwebview

这对某些专家来说可能很容易,所以。 Xcode可视化界面中没有用于故事板使用的WKWebView,而不是UIWebview,因此必须以编程方式创建。我使用导航控制器为我的整个应用程序。

我在故事板中创建了一个VIEW,并使用自动布局来约束0,0,0,0。所以想将WKWebView添加到该视图" Daview"。作为子视图并完全填写。我似乎无法弄明白。

请查看下面的代码

class ViewController2: UIViewController ,WKNavigationDelegate {

    @IBOutlet weak var navigation: UINavigationItem!

    @IBOutlet var daview: UIView!
    var restWeb: WKWebView?

    @IBOutlet var BottomView: UIView!

   // var ActivityIndicator =  UIActivityIndicatorView()


     override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(true)       
         println(" daview bounds in VIEWDIDAPPEAR is \(self.daview.bounds) y Frame \(self.daview.frame)")
       restWeb!.bounds = daview.bounds
    }



    override func viewDidLoad() {
        super.viewDidLoad()

        /* Create our preferences on how the web page should be loaded */
        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true

        /* Create a configuration for our preferences */
        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences


        /* Now instantiate the web view */
        restWeb = WKWebView(frame: self.daview.bounds, configuration: configuration)

   let urlString = "http://www.google.com"  


        println("EL DETAIL URL es \(urlString)")
        if let theWebView = restWeb {
            println("Existe restWeb")
        let urlRest = NSURL(string:urlString)
        let requestRest = NSURLRequest(URL: urlRest!)
        theWebView.loadRequest(requestRest)
        theWebView.allowsBackForwardNavigationGestures = true
        theWebView.navigationDelegate = self
        self.daview.addSubview(theWebView)
        }


} // finish viewdidload

4 个答案:

答案 0 :(得分:12)

一行答案:将代码从viewDidLoad()移至viewDidAppear()

说明:我不是专家,但我早年遇到过这个问题。 您的webview的问题是您正在尝试在viewDidLoad()

中设置边界
    restWeb = WKWebView(frame: self.daview.bounds, configuration: configuration)

但是,只有在视图出现时才会完成自动布局。意味着,你试图在viewDidLoad中使用的边界(self.daview.bounds),甚至在设置视图布局之前就会给你一个值。

解决方案:viewDidAppear中和之后都可以使用正确的自动布局边界。如果您移动代码

  

WKWebView(框架:self.daview.bounds,配置:配置)

从viewDidLoad()到viewDidAppear(),那么你的所有愿望都会成真。

答案 1 :(得分:2)

  1. 创建一种方法,以编程方式设置4个自动布局约束:

    -(void)setWebViewConstraints {
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.bottomLayoutGuide attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.topLayoutGuide attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0]];
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0]];
        [self.view addConstraint:[NSLayoutConstraint constraintWithItem:self.webView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0]];
    }
    
  2. 在viewDidAppear中调用该方法:

    -(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self setWebViewConstraints]; }
    

答案 2 :(得分:2)

要在自动布局中使用WKWebView,请在xib / storyboard编辑器中创建UIView,将类更改为WKWebView。添加autolayout constrints。 在viewDidLoad方法中手动删除标志translatesAutoresizingMaskIntoConstraints

- (void) viewDidLoad
{
   // ...
    _webView.translatesAutoresizingMaskIntoConstraints = NO; // This is necessary!
   // ...
}

// Addition for initialize WKWebView from StoryBoard.
- (instancetype) initWithCoder:(NSCoder*)coder
{
    UIView* view = [UIView.alloc initWithCoder:coder];
    ASSERT(view != nil);
    self = [self initWithFrame:view.frame configuration:WKWebViewConfiguration.new];
    return self;
}

答案 3 :(得分:0)

另一种使您能够在IB中为WKWebView使用自动布局的方法是将集合视图拖到ViewController中。然后为与Collection View关联的UIViewController创建一个swift文件。将所有WKWebView代码放在此UIViewController中。使用自动布局将集合视图放置在包含它的UIViewController中的任何位置。