Swift - 给UIWebView Scroll View添加约束,使得到屏幕边缘的距离四周都是0
Swift - Add constraints to UIWebView Scroll View so that the distance from the screen edges is 0 all around
我正在向我的视图中添加一个 webview,如下所示:
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]
我将向 UIWebView 的 scrollView 添加子视图,现在我需要向 scrollview 添加约束,以便与屏幕边缘的距离周围为 0。
我的问题是我该怎么做,因为我是约束的新手?
我需要以编程方式执行此操作
我试过示例一来回答你的问题
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)
}
}
我正在向我的视图中添加一个 webview,如下所示:
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]
我将向 UIWebView 的 scrollView 添加子视图,现在我需要向 scrollview 添加约束,以便与屏幕边缘的距离周围为 0。
我的问题是我该怎么做,因为我是约束的新手?
我需要以编程方式执行此操作
我试过示例一来回答你的问题
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)
}
}