我在我的视图中添加了一个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。
我的问题是我如何做到这一点,因为我对限制是全新的?
我需要以编程方式
答案 0 :(得分:1)
我为你的问题试了一个样本
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)
}
}