0
votes

I have a ViewController with a UIWebView, that loads a Youtube video. When I try to watch the video in fullscreen mode I get constraint errors. The video plays fine if it's not fullscreen. When I press on the fullscreen button, the video goes to fullscreen for a split second and then go back to normal mode and restart the video from the beginning. Not sure why I'm getting these errors.

Here is my ViewController class:

class VideoViewController: UIViewController {
    private var webView: UIWebView!
    var videoId: String? = "4YpZg-XpdAY"

    override func viewDidLoad() {
        super.viewDidLoad()

        webView = UIWebView()
        webView.allowsInlineMediaPlayback = true
        webView.translatesAutoresizingMaskIntoConstraints = true
        view.addSubview(webView)
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if let url = URL(string: "https://www.youtube.com/watch?v=\(videoId!)") {
            webView.loadRequest(URLRequest(url: url))
        }

        view.addConstraintsWithFormat(format: "H:|[v0]|", views: webView)
        view.addConstraintsWithFormat(format: "V:|[v0]|", views: webView)
    }
}

Error I'm getting in Console:

2018-10-16 02:02:21.662220-0400 Logic[2845:38866] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSAutoresizingMaskLayoutConstraint:0x604000290040 h=-&- v=-&- AVPlaybackControlsView:0x7fd920c88770.width == AVPlayerViewControllerContentView:0x7fd92317b2a0.width   (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x60c000094870 h=-&- v=-&- AVPlayerViewControllerContentView:0x7fd92317b2a0.width == UIView:0x7fd920e6e490.width   (active)>",
"<NSLayoutConstraint:0x60c0000975c0 AVBackdropView:0x7fd920c89db0.leading == UILayoutGuide:0x60c0001b81e0'UIViewLayoutMarginsGuide'.leading   (active)>",
"<NSLayoutConstraint:0x60c000097660 AVBackdropView:0x7fd920c8d9d0.trailing == UILayoutGuide:0x60c0001b81e0'UIViewLayoutMarginsGuide'.trailing   (active)>",
"<NSLayoutConstraint:0x60c000097980 H:[AVBackdropView:0x7fd920c89db0]-(>=11)-[AVBackdropView:0x7fd920c8d9d0]   (active)>",
"<NSLayoutConstraint:0x60c000099140 'UIView-Encapsulated-Layout-Width' UIView:0x7fd920e6e490.width == 0   (active)>",
"<NSLayoutConstraint:0x60c000097430 'UIView-leftMargin-guide-constraint' H:|-(6)-[UILayoutGuide:0x60c0001b81e0'UIViewLayoutMarginsGuide'](LTR)   (active, names: '|':AVPlaybackControlsView:0x7fd920c88770 )>",
"<NSLayoutConstraint:0x60c0000974d0 'UIView-rightMargin-guide-constraint' H:[UILayoutGuide:0x60c0001b81e0'UIViewLayoutMarginsGuide']-(6)-|(LTR)   (active, names: '|':AVPlaybackControlsView:0x7fd920c88770 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x60c000097980 H:[AVBackdropView:0x7fd920c89db0]-(>=11)-[AVBackdropView:0x7fd920c8d9d0]   (active)>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2018-10-16 02:02:21.663574-0400 Logic[2845:38866] [LayoutConstraints] Unable to simultaneously satisfy constraints.
Probably at least one of the constraints in the following list is one you don't want. 
Try this: 
    (1) look at each constraint and try to figure out which you don't expect; 
    (2) find the code that added the unwanted constraint or constraints and fix it. 
(Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
"<NSAutoresizingMaskLayoutConstraint:0x604000290040 h=-&- v=-&- AVPlaybackControlsView:0x7fd920c88770.width == AVPlayerViewControllerContentView:0x7fd92317b2a0.width   (active)>",
"<NSAutoresizingMaskLayoutConstraint:0x60c000094870 h=-&- v=-&- AVPlayerViewControllerContentView:0x7fd92317b2a0.width == UIView:0x7fd920e6e490.width   (active)>",
"<NSLayoutConstraint:0x60c000097160 AVView:0x7fd920c8b170.width == AVTransportControlsView:0x7fd920c8b7b0.width   (active)>",
"<NSLayoutConstraint:0x60c000097840 AVView:0x7fd920c8b170.width <= AVPlaybackControlsView:0x7fd920c88770.width   (active)>",
"<NSLayoutConstraint:0x600000281040 AVBackdropView:0x7fd92318cc10.width <= AVTransportControlsView:0x7fd920c8b7b0.width   (active)>",
"<NSLayoutConstraint:0x600000281450 H:|-(16)-[Time Elapsed](LTR)   (active, names: Time Elapsed:0x7fd923134e60, '|':AVBackdropView:0x7fd92318cc10 )>",
"<NSLayoutConstraint:0x6000002814a0 Time Remaining.right == AVBackdropView:0x7fd92318cc10.right - 16   (active, names: Time Remaining:0x7fd920c460d0 )>",
"<NSLayoutConstraint:0x6000002815e0 AVScrubber:0x7fd920c8beb0.left == Time Elapsed.left   (active, names: Time Elapsed:0x7fd923134e60 )>",
"<NSLayoutConstraint:0x600000281630 AVScrubber:0x7fd920c8beb0.right == Time Remaining.right   (active, names: Time Remaining:0x7fd920c460d0 )>",
"<NSLayoutConstraint:0x60c000099140 'UIView-Encapsulated-Layout-Width' UIView:0x7fd920e6e490.width == 0   (active)>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x600000281630 AVScrubber:0x7fd920c8beb0.right == Time Remaining.right   (active, names: Time Remaining:0x7fd920c460d0 )>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
1
@TamásSengel I've tried that solution but its still giving me the same errornewguy
As this answer says, the constraint errors are very likely a bug by Apple and you can't do anything with them.Tamás Sengel

1 Answers

0
votes
Try this


var videoID: String? = "4YpZg-XpdAY"

 guard let youtubeURL = URL(string: "https://www.youtube.com/embed/\(videoID)?rel=0&controls=0&showinfo=0&autoplay=1&allowfullscreen=1")
            else { return }
        webView.allowsInlineMediaPlayback = false
        webView.mediaPlaybackRequiresUserAction = false
        webView.mediaPlaybackAllowsAirPlay = true
        webView.loadRequest( URLRequest(url: youtubeURL) )