我在 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
}
最佳答案
设置这些约束
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/