0

I am adding a webview to my view like so:

webview = UIWebView()

        webview.frame = self.view.bounds
        webview.scrollView.frame = webview.frame

        webview.userInteractionEnabled = true
        webview.scalesPageToFit = true
        webview.becomeFirstResponder()
        webview.delegate = self
        webview.scrollView.delegate = self
        self.view.addSubview(webview)

        webview.loadRequest(NSURLRequest(URL:url))
        webview.gestureRecognizers = [pinchRecognizer, panRecognizer]

I will be adding subviews to the UIWebView's scrollView, now I need to add constraints to the scrollview so that the distance from the screen edges is 0 all around.

My question is how would I do this, as I am brand new to constraints?

I need to this programmatically

user979331
  • 11,039
  • 73
  • 223
  • 418

1 Answers1

1

I tried sample one for your question

import UIKit

class ViewController: UIViewController {

@IBOutlet var webViewDynamicHeight: UIWebView!

var horizontalConstraint : NSLayoutConstraint!
var verticalConstraint: NSLayoutConstraint!
var widthConstraint : NSLayoutConstraint!
var heightConstraint : NSLayoutConstraint!


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    webViewDynamicHeight.loadRequest(NSURLRequest(URL: NSURL(string: "https://www.google.de/intl/de/policies/terms/regional.html")!))

}

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

func webViewDidFinishLoad(webView: UIWebView) {

    let height:CGFloat = webView.scrollView.contentSize.height
    print("The webView height is -: \(height)")

    let width:CGFloat = webView.scrollView.contentSize.width
    print("The webView height is -: \(width)")


    horizontalConstraint = NSLayoutConstraint(item:webView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)
    webView.addConstraint(horizontalConstraint)

    verticalConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0)
    webView.addConstraint(verticalConstraint)


    widthConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100)
    widthConstraint.constant = width
    webView.addConstraint(widthConstraint)

    heightConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.Height, multiplier: 1.0, constant: height)
    heightConstraint.constant = height
    webView.addConstraint(heightConstraint)

}
}
user3182143
  • 9,459
  • 3
  • 32
  • 39