【问题标题】:Swift - Add constraints to UIWebView Scroll View so that the distance from the screen edges is 0 all aroundSwift - 向 UIWebView Scroll View 添加约束,以便与屏幕边缘的距离全部为 0
【发布时间】:2016-07-26 19:01:14
【问题描述】:

我正在向我的视图添加一个 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。

我的问题是如何做到这一点,因为我是全新的约束?

我需要以编程方式进行此操作

【问题讨论】:

  • 不能在 GUI 中分配约束吗?这样做似乎要简单得多。
  • 我必须以编程方式完成此操作
  • 请把你的问题说清楚,你想在 webview 和 webview 持有者之间或 webview 的滚动视图和你在滚动视图上添加的子视图之间进行约束吗?

标签: ios objective-c swift uiwebview


【解决方案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)

}
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 2017-05-28
    • 2014-09-18
    • 1970-01-01
    相关资源
    最近更新 更多