Swift 2编程约束,我无法看到Web视图

时间:2015-10-25 10:27:58

标签: swift constraints swift2 wkwebview

我有一个webkit,在webkit下有另一个视图......所以我有三个view = view,secondView(一个包含WKWebView)和bannerView(带ADS)。我以编程方式将WKWebView添加到secondView,但bannerView及其约束在storyboard中设置。现在我需要为WKWebView设置约束,我看到了数百个教程,运行没有崩溃和问题,但我看不到WKWebView!我该如何解决这个问题?

代码:

    class ViewController: UIViewController {

    @IBOutlet var bannerView: GADBannerView!



    @IBOutlet var secondView: UIView!
       var webView = WKWebView()
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

        print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())






        //bannerView.adUnitID = ""
        //bannerView.rootViewController = self
        //var request = GADRequest()
        //request.testDevices = ["kGADSimulatorID"]
        //bannerView.loadRequest(request)



        let preferences = WKPreferences()
        preferences.javaScriptEnabled = true

        let configuration = WKWebViewConfiguration()
        configuration.preferences = preferences

        webView.frame = CGRectMake(0, 0, 600, 543)


            webView.customUserAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/600.7.12 (KHTML, like Gecko) Version/8.0.7 Safari/600.7.12"




        let url = NSURL(string:"https://apple.com/")
        let req = NSURLRequest(URL:url!)
        self.webView.loadRequest(req)

        secondView.addSubview(webView)
        secondView.bringSubviewToFront(webView)
        webView.translatesAutoresizingMaskIntoConstraints = false

        let xConstraint = NSLayoutConstraint(item: webView, attribute: .CenterX, relatedBy: .Equal, toItem: secondView, attribute: .CenterX, multiplier: 1, constant: 0)

        let yConstraint = NSLayoutConstraint(item: webView, attribute: .CenterY, relatedBy: .Equal, toItem: secondView, attribute: .CenterY, multiplier: 1, constant: 0)

let bottomConstraint = NSLayoutConstraint(item: webView, attribute: .Bottom, relatedBy: .Equal, toItem: secondView, attribute: .Bottom, multiplier: 1, constant: 0)

        secondView.addConstraints([xConstraint, yConstraint, bottomConstraint])

       /*
        let topConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.TopMargin, relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1, constant: 0)

        let leadingConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Leading , relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: -20)

       let trailingConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Leading , relatedBy: NSLayoutRelation.Equal, toItem: self.view, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: -20)

      let bottomConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Bottom , relatedBy: NSLayoutRelation.Equal, toItem: self.bannerView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)

        */


     /*   var LeftwebViewConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.Leading, multiplier: 1.0, constant: -20)

        var RightwebViewConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.TrailingMargin, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.TrailingMargin, multiplier: 1.0, constant: -20)

        var BottomwebViewConstraints = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: bannerView, attribute: NSLayoutAttribute.BottomMargin, multiplier: 1.0, constant: 0)


       // view.addConstraints([LeftwebViewConstraints, RightwebViewConstraints, BottomwebViewConstraints])
         NSLayoutConstraint.activateConstraints([LeftwebViewConstraints, RightwebViewConstraints, BottomwebViewConstraints])


        */


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }




}

1 个答案:

答案 0 :(得分:0)

是的,您似乎缺少一些约束。

我可能建议使用VFL添加所有必要的约束,这可能更直观:

secondView.addSubview(webView)
webView.translatesAutoresizingMaskIntoConstraints = false
let views = ["webView" : webView]
secondView.addSubview(webView)
secondView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|[webView]|", options: [], metrics: nil, views: views))
secondView.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|[webView]|", options: [], metrics: nil, views: views))

这显然假设首先正确定义了secondView的约束。在frame的{​​{1}}开始失眠之前,请检查其frame

但请注意,我不会手动设置webView的{​​{1}},而是简单地定义了水平和垂直约束。