ios - 添加 WkWebView 约束

标签 ios swift wkwebview

我在 nib 中添加了一个 wkwebView。我无法设置从 wkWebView 到 contentView (BlueView) 的大小。 BlueView 的约束是正确的。这是我的代码:

感谢帮助

@IBOutlet weak var view: UIView!
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var stopButton: UIButton!
@IBOutlet weak var skipButton: UIButton!
@IBOutlet weak var infoLabel: UILabel!

var webView = WKWebView(frame: .zero)

override func awakeFromNib() {

    webView.frame.size = CGSize(width: contentView.bounds.width, height: contentView.bounds.height)
    contentView.addSubview(webView)

    let myURL = URL(string: "https://www.apple.com")
    let myRequest = URLRequest(url: myURL!)
    webView.load(myRequest)

}

override init(frame: CGRect) {
    super.init(frame: frame)
    xibSetup()
}

required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)
    xibSetup()

}

func xibSetup() {
    view = loadViewFromNib()

    // use bounds not frame or it'll be offset
    view.frame = bounds

    // Make the view stretch with containing view
    view.autoresizingMask = [UIViewAutoresizing.flexibleWidth, UIViewAutoresizing.flexibleHeight]

    // Adding custom subview on top of our view (over any custom drawing > see note below)
    addSubview(view)

}
 func loadViewFromNib() -> UIView! {

    let bundle = Bundle(for: type(of: self))
    let nib = UINib(nibName: "ReCaptchaV2", bundle: bundle)
    let view = nib.instantiate(withOwner: self, options: nil).first as! UIView

    return view
}

enter image description here

enter image description here

最佳答案

设置这些约束

override func awakeFromNib() {

   contentView.addSubview(webView)

   webView.translatesAutoresizingMaskIntoConstraints = false

   webView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 0).isActive = true

   webView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: 0).isActive = true

   webView.topAnchor.constraint(equalTo: contentView.topAnchor, constant: 0).isActive = true

   webView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor, constant: 0).isActive = true

  let myURL = URL(string: "https://www.apple.com")

  let myRequest = URLRequest(url: myURL!)

  webView.load(myRequest)

 }

关于ios - 添加 WkWebView 约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49700667/

相关文章:

iOS动画/变形形状从圆形到方形

swift - 使用按钮将文本输入到文本字段?

swift - 以编程方式创建 SKScene 子类,没有大小信息?

ios - 在 WKWebview 中隐藏网页上的特定 DIV

swift - 如何使用 Swift 将 Angular routerLink 传递到 WKWebview

ios - 我是否需要 Facebook 应用程序才能让我的 iOS 应用程序在某人的墙上发帖?

ios - 核心数据 - 如果在保存完成前删除应用程序崩溃

ios - 特定日期格式的日期格式化程序

ios - CGRectContainsPoint 不适用于不同的 View

ios - 使用 KVO 观察 WKWebView 的 URL 属性在 iOS 10 中不起作用