Reputation: 47
I added a wkwebView into a nib. I can not set the size from the wkWebView to the contentView (BlueView). The constraints from the BlueView are correct. Here my code:
thanks for helping
@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
}
Upvotes: 3
Views: 4831
Reputation: 14123
Set the constraints from the parent view, not from safe area. And keep only 4 constraints. So :
trailing constraint from parent view = 0
bottom constraint from button = 0
top constraint from parent view = 0
left constraint from parent view = 0
Check these screenshots :
1. Constraints :
2. Parent view selected in constraint :
3. Output :
Upvotes: 1
Reputation: 100549
Set these constraints
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)
}
Upvotes: 6