I have a UIWebView included in a UIViewController which is a descendant of UINavigationController. It looks like this:
我有一个UIViewController中包含的UIWebView,它是UINavigationController的后代。它看起来像这样:
The app is portrait only. When I play the video I want the user to be able to rotate the device and see the video in landscape mode. I use this code to allow it:
该应用程序仅限肖像。当我播放视频时,我希望用户能够旋转设备并以横向模式观看视频。我使用这段代码来允许它:
- (NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window
{
id presentedViewController = [self topMostController];
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if ([className isEqualToString:@"MPInlineVideoFullscreenViewController"] ||
[className isEqualToString:@"MPMoviePlayerViewController"] ||
[className isEqualToString:@"AVFullScreenViewController"]) {
return UIInterfaceOrientationMaskAllButUpsideDown;
}
return UIInterfaceOrientationMaskPortrait;
}
- (UIViewController *)topMostController {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
And then in my UINavigationController (so when the video finishes the view is not presented in landscape but only in portrait):
然后在我的UINavigationController中(所以当视频结束时,视图不是以横向呈现,而是仅以纵向呈现):
- (BOOL)shouldAutorotate
{
return NO;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
Everything works perfectly:
一切都很完美:
But then the video is done playing (or the user taps ‘Done’) and the screens return to the underlying view, this is what happens:
但是视频完成播放(或者用户点击“完成”)并且屏幕返回基础视图,这就是发生的情况:
As you can see, the navigation bar slips under the status bar. Additionally, I get a lot of auto-layout errors in the logs: http://pastebin.com/09xHzmgJ
如您所见,导航栏在状态栏下滑动。另外,我在日志中遇到了很多自动布局错误:http://pastebin.com/09xHzmgJ
Any idea about how to solve this?
有关如何解决这个问题的任何想法?
8 个解决方案
#1
17
I temporarily solved (through a hack) with the following code in the viewDidLoad
of my controller. I have to specify that the code is specifically made for my case: since I explicitly disallow landscape orientation of my UINavigationController (see code above), the usual notification “UIDeviceOrientationDidChange” is not called when the playback finished and the window goes back to portrait. However, I hope there is a better option and this is a bug of the SDK, since it does not appear on iOS 7 and given the amount of auto-layout errors I get related to the video player (on which I have no control).
我在控制器的viewDidLoad中使用以下代码暂时解决(通过hack)。我必须指定代码专门针对我的情况:因为我明确禁止我的UINavigationController的横向方向(参见上面的代码),当回放结束并且窗口返回到肖像时,通常不会调用通知“UIDeviceOrientationDidChange”。但是,我希望有一个更好的选择,这是SDK的一个错误,因为它没有出现在iOS 7上,并且我得到了与视频播放器相关的自动布局错误(我无法控制) 。
- (void)viewDidLoad
{
[super viewDidLoad];
// […]
/*
Hack to fix navigation bar position/height on iOS 8 after closing fullscreen video
Observe for “UIWindowDidRotateNotification” since “UIDeviceOrientationDidChangeNotification” is not called in the present conditions
Check if the notification key (“UIWindowOldOrientationUserInfoKey”) in userInfo is either 3 or 4, which means the old orientation was landscape
If so, correct the frame of the navigation bar to the proper size.
*/
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
if ([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) {
self.navigationController.navigationBar.frame = (CGRect){0, 0, self.view.frame.size.width, 64};
}
}];
}
And then…
接着…
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"UIWindowDidRotateNotification"];
}
#2
5
I encountered exactly the same issue and used the same code as @entropid did. However the accepted solution did not work for me.
我遇到了完全相同的问题,并使用了与@entropid相同的代码。然而,接受的解决方案对我不起作用。
It took me hours to come up with the following one-line fix that made things working for me:
花了我几个小时来提出以下一线修复,使我的工作有用:
- (void)viewWillLayoutSubviews {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
#3
4
I faced this problem yesterday, where @entropid answer worked for iOS 9 and below, but for iOS 10 it didn't (since iOS 10 did actually hide the status bar, where on iOS 9 and below it was just the UINavigationBar
that changed its frame without hiding the status bar and, thus, it overlapped that bar).
我昨天遇到了这个问题,其中@entropid的答案适用于iOS 9及以下,但对于iOS 10它没有(因为iOS 10确实隐藏了状态栏,在iOS 9及以下它只是改变了它的UINavigationBar帧没有隐藏状态栏,因此,它与该栏重叠)。
Also, subscribing to MPMoviePlayerControllerDidExitFullScreen
notification didn't work either, sometimes it simply wasn't called (in my particular case, it was because it was a video from a UIWebView
, which used a different class of player which looks similar to MPMoviePlayerController
).
此外,订阅MPMoviePlayerControllerDidExitFullScreen通知也不起作用,有时它根本没有被调用(在我的特定情况下,这是因为它是来自UIWebView的视频,它使用了类似于MPMoviePlayerController的不同类别的播放器)。
So I solved it using a solution like the one @StanislavPankevich suggested, but I subscribed to notifications when a UIWindow
has become hidden (which can be in several cases, like when a video has finished, but also when a UIActivityViewController
dismisses and other cases) instead of viewWillLayoutSubviews
. For my particular case (a subclass of UINavigationController
), methods like viewDidAppear
, viewWillAppear
, etc. simply weren't being called.
所以我使用类似@StanislavPankevich建议的解决方案来解决它,但是当UIWindow被隐藏时我订阅了通知(在几种情况下,例如视频完成时,以及UIActivityViewController解散和其他情况时)而不是viewWillLayoutSubviews。对于我的特定情况(UINavigationController的子类),viewDidAppear,viewWillAppear等方法根本就没有被调用。
viewDidLoad
viewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoDidExitFullscreen:)
name:UIWindowDidBecomeHiddenNotification
object:nil];
// Some other logic...
}
dealloc
的dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
And finally, videoDidExitFullscreen
最后,videoDidExitFullscreen
- (void)videoDidExitFullscreen:(NSNotification *)notification {
// You would want to check here if the window dismissed was actually a video one or not.
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationFade];
}
I used UIStatusBarAnimationFade
because it looks a lot smoother than UIStatusBarAnimationNone
, at least from a user perspective.
我使用了UIStatusBarAnimationFade,因为它看起来比UIStatusBarAnimationNone更平滑,至少从用户的角度来看。
#4
3
Swift version:
Swift版本:
//AppDelegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
var presentedVC = application.keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController
{
presentedVC = pVC
}
if let pVC = presentedVC
{
if contains(["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"], pVC.nameOfClass)
{
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
}
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
//Extension:
public extension NSObject{
public class var nameOfClass: String{
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
public var nameOfClass: String{
return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
}
}
//View controller:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
override func viewWillLayoutSubviews() {
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.None)
}
#5
1
It's very simple, as it says @Stanislav Pankevich, but in
这很简单,正如@Stanislav Pankevich所说,但是在
swift 3 version
swift 3版
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews();
UIApplication.shared.isStatusBarHidden = false
}
#6
0
I was having the same issue and using @entropid solution I was able to fix the navigation bar problem. But my view remains below the nav bar which I fix using "sizeToFit".
我遇到了同样的问题并使用@entropid解决方案我能够解决导航栏问题。但我的视图仍然在我使用“sizeToFit”修复的导航栏下方。
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
if ([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) {
[self.navigationController.navigationBar sizeToFit];
self.navigationController.navigationBar.frame = (CGRect){0, 0, self.view.frame.size.width, 64};
}
}];
I haven't tested it properly but its working for me right now
我没有正确测试它,但它现在正在为我工作
#7
0
On iOS 11 accepted solution didn't work for me. Seems that navigation bar stops reflecting for frame changes. But there is a workaround. At first, we need to modify supportedInterfaceOrientationsForWindow
method to return UIInterfaceOrientationMaskLandscape
for video controllers instead of UIInterfaceOrientationMaskAllButUpsideDown
. In my case when I tap on embedded YouTube video, system always opens AVFullScreenViewController, so I removed other checks from original example. Code:
在iOS 11上,接受的解决方案对我不起作用。似乎导航栏停止反映帧更改。但有一个解决方法。首先,我们需要修改supportedInterfaceOrientationsForWindow方法,以返回视频控制器的UIInterfaceOrientationMaskLandscape,而不是UIInterfaceOrientationMaskAllButUpsideDown。在我的情况下,当我点击嵌入的YouTube视频时,系统总是打开AVFullScreenViewController,所以我从原始示例中删除了其他检查。码:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
__kindof UIViewController *presentedViewController = [self topMostController];
// Allow rotate videos
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if ([className isEqualToString:@"AVFullScreenViewController"]) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
This didn't changes behaviour of AVFullScreenViewController on iOS 10 and less, but fixes navigation bar on iOS 11, so there is no need to update frame (also there is a side-effect on iOS 11 that video rotates from landscape when starts playing, but it's a tradeoff). Next, we need to add check in UIWindowDidBecomeHiddenNotification
method:
这并没有改变iOS 10及更低版本的AVFullScreenViewController的行为,但修复了iOS 11上的导航栏,因此无需更新帧(iOS 11上也有一个副作用,视频在开始播放时从横向旋转,但这是一个权衡)。接下来,我们需要在UIWindowDidBecomeHiddenNotification方法中添加check:
- (void)videoDidExitFullscreen {
if (@available(iOS 11, *)) {
// Fixes status bar on iPhone X
[self setNeedsStatusBarAppearanceUpdate];
} else {
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, statusAndNavBarHeight);
}
}
Without setNeedsStatusBarAppearanceUpdate
text in status bar will not appear on iPhone X, for other devices it's not needed.
如果没有setNeedsStatusBarAppearanceUpdate状态栏中的文本将不会出现在iPhone X上,对于其他设备则不需要。
#8
-1
My answer on this question works great. Here It will play the video inside your WebView normally but if you tilt your phone it will play in Landscape!
我对这个问题的答案很有用。这里它会正常播放你的WebView中的视频,但如果你倾斜你的手机,它将在风景中播放!
Also important to note is if you include youtube.com as the Base URL it will load much quicker.
另外需要注意的是,如果您将youtube.com作为基本URL包含,它将加载得更快。
Make a UIWebView in your storyboard and connect the @property to it, then reference below.
在故事板中创建一个UIWebView并将@property连接到它,然后在下面引用。
CGFloat width = self.webView.frame.size.height;
CGFloat height = self.webView.frame.size.width;
NSString *youTubeVideoCode = @"dQw4w9WgXcQ";
NSString *embedHTML = @"<iframe width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\" style=\"margin:-8px;padding:0;\" allowfullscreen></iframe>";
NSString *html = [NSString stringWithFormat:embedHTML, width, height, youTubeVideoCode];
self.webView.scrollView.bounces = NO;
[self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];
#1
17
I temporarily solved (through a hack) with the following code in the viewDidLoad
of my controller. I have to specify that the code is specifically made for my case: since I explicitly disallow landscape orientation of my UINavigationController (see code above), the usual notification “UIDeviceOrientationDidChange” is not called when the playback finished and the window goes back to portrait. However, I hope there is a better option and this is a bug of the SDK, since it does not appear on iOS 7 and given the amount of auto-layout errors I get related to the video player (on which I have no control).
我在控制器的viewDidLoad中使用以下代码暂时解决(通过hack)。我必须指定代码专门针对我的情况:因为我明确禁止我的UINavigationController的横向方向(参见上面的代码),当回放结束并且窗口返回到肖像时,通常不会调用通知“UIDeviceOrientationDidChange”。但是,我希望有一个更好的选择,这是SDK的一个错误,因为它没有出现在iOS 7上,并且我得到了与视频播放器相关的自动布局错误(我无法控制) 。
- (void)viewDidLoad
{
[super viewDidLoad];
// […]
/*
Hack to fix navigation bar position/height on iOS 8 after closing fullscreen video
Observe for “UIWindowDidRotateNotification” since “UIDeviceOrientationDidChangeNotification” is not called in the present conditions
Check if the notification key (“UIWindowOldOrientationUserInfoKey”) in userInfo is either 3 or 4, which means the old orientation was landscape
If so, correct the frame of the navigation bar to the proper size.
*/
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
if ([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) {
self.navigationController.navigationBar.frame = (CGRect){0, 0, self.view.frame.size.width, 64};
}
}];
}
And then…
接着…
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self forKeyPath:@"UIWindowDidRotateNotification"];
}
#2
5
I encountered exactly the same issue and used the same code as @entropid did. However the accepted solution did not work for me.
我遇到了完全相同的问题,并使用了与@entropid相同的代码。然而,接受的解决方案对我不起作用。
It took me hours to come up with the following one-line fix that made things working for me:
花了我几个小时来提出以下一线修复,使我的工作有用:
- (void)viewWillLayoutSubviews {
[[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
}
#3
4
I faced this problem yesterday, where @entropid answer worked for iOS 9 and below, but for iOS 10 it didn't (since iOS 10 did actually hide the status bar, where on iOS 9 and below it was just the UINavigationBar
that changed its frame without hiding the status bar and, thus, it overlapped that bar).
我昨天遇到了这个问题,其中@entropid的答案适用于iOS 9及以下,但对于iOS 10它没有(因为iOS 10确实隐藏了状态栏,在iOS 9及以下它只是改变了它的UINavigationBar帧没有隐藏状态栏,因此,它与该栏重叠)。
Also, subscribing to MPMoviePlayerControllerDidExitFullScreen
notification didn't work either, sometimes it simply wasn't called (in my particular case, it was because it was a video from a UIWebView
, which used a different class of player which looks similar to MPMoviePlayerController
).
此外,订阅MPMoviePlayerControllerDidExitFullScreen通知也不起作用,有时它根本没有被调用(在我的特定情况下,这是因为它是来自UIWebView的视频,它使用了类似于MPMoviePlayerController的不同类别的播放器)。
So I solved it using a solution like the one @StanislavPankevich suggested, but I subscribed to notifications when a UIWindow
has become hidden (which can be in several cases, like when a video has finished, but also when a UIActivityViewController
dismisses and other cases) instead of viewWillLayoutSubviews
. For my particular case (a subclass of UINavigationController
), methods like viewDidAppear
, viewWillAppear
, etc. simply weren't being called.
所以我使用类似@StanislavPankevich建议的解决方案来解决它,但是当UIWindow被隐藏时我订阅了通知(在几种情况下,例如视频完成时,以及UIActivityViewController解散和其他情况时)而不是viewWillLayoutSubviews。对于我的特定情况(UINavigationController的子类),viewDidAppear,viewWillAppear等方法根本就没有被调用。
viewDidLoad
viewDidLoad中
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(videoDidExitFullscreen:)
name:UIWindowDidBecomeHiddenNotification
object:nil];
// Some other logic...
}
dealloc
的dealloc
- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
}
And finally, videoDidExitFullscreen
最后,videoDidExitFullscreen
- (void)videoDidExitFullscreen:(NSNotification *)notification {
// You would want to check here if the window dismissed was actually a video one or not.
[[UIApplication sharedApplication] setStatusBarHidden:NO
withAnimation:UIStatusBarAnimationFade];
}
I used UIStatusBarAnimationFade
because it looks a lot smoother than UIStatusBarAnimationNone
, at least from a user perspective.
我使用了UIStatusBarAnimationFade,因为它看起来比UIStatusBarAnimationNone更平滑,至少从用户的角度来看。
#4
3
Swift version:
Swift版本:
//AppDelegate:
func application(application: UIApplication, supportedInterfaceOrientationsForWindow window: UIWindow?) -> Int {
var presentedVC = application.keyWindow?.rootViewController
while let pVC = presentedVC?.presentedViewController
{
presentedVC = pVC
}
if let pVC = presentedVC
{
if contains(["MPInlineVideoFullscreenViewController", "MPMoviePlayerViewController", "AVFullScreenViewController"], pVC.nameOfClass)
{
return Int(UIInterfaceOrientationMask.AllButUpsideDown.rawValue)
}
}
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
//Extension:
public extension NSObject{
public class var nameOfClass: String{
return NSStringFromClass(self).componentsSeparatedByString(".").last!
}
public var nameOfClass: String{
return NSStringFromClass(self.dynamicType).componentsSeparatedByString(".").last!
}
}
//View controller:
override func supportedInterfaceOrientations() -> Int {
return Int(UIInterfaceOrientationMask.Portrait.rawValue)
}
override func preferredInterfaceOrientationForPresentation() -> UIInterfaceOrientation {
return UIInterfaceOrientation.Portrait
}
override func shouldAutorotate() -> Bool {
return false
}
override func viewWillLayoutSubviews() {
UIApplication.sharedApplication().setStatusBarHidden(false, withAnimation: UIStatusBarAnimation.None)
}
#5
1
It's very simple, as it says @Stanislav Pankevich, but in
这很简单,正如@Stanislav Pankevich所说,但是在
swift 3 version
swift 3版
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews();
UIApplication.shared.isStatusBarHidden = false
}
#6
0
I was having the same issue and using @entropid solution I was able to fix the navigation bar problem. But my view remains below the nav bar which I fix using "sizeToFit".
我遇到了同样的问题并使用@entropid解决方案我能够解决导航栏问题。但我的视图仍然在我使用“sizeToFit”修复的导航栏下方。
[[NSNotificationCenter defaultCenter] addObserverForName:@"UIWindowDidRotateNotification" object:nil queue:nil usingBlock:^(NSNotification *note) {
if ([note.userInfo[@"UIWindowOldOrientationUserInfoKey"] intValue] >= 3) {
[self.navigationController.navigationBar sizeToFit];
self.navigationController.navigationBar.frame = (CGRect){0, 0, self.view.frame.size.width, 64};
}
}];
I haven't tested it properly but its working for me right now
我没有正确测试它,但它现在正在为我工作
#7
0
On iOS 11 accepted solution didn't work for me. Seems that navigation bar stops reflecting for frame changes. But there is a workaround. At first, we need to modify supportedInterfaceOrientationsForWindow
method to return UIInterfaceOrientationMaskLandscape
for video controllers instead of UIInterfaceOrientationMaskAllButUpsideDown
. In my case when I tap on embedded YouTube video, system always opens AVFullScreenViewController, so I removed other checks from original example. Code:
在iOS 11上,接受的解决方案对我不起作用。似乎导航栏停止反映帧更改。但有一个解决方法。首先,我们需要修改supportedInterfaceOrientationsForWindow方法,以返回视频控制器的UIInterfaceOrientationMaskLandscape,而不是UIInterfaceOrientationMaskAllButUpsideDown。在我的情况下,当我点击嵌入的YouTube视频时,系统总是打开AVFullScreenViewController,所以我从原始示例中删除了其他检查。码:
- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
__kindof UIViewController *presentedViewController = [self topMostController];
// Allow rotate videos
NSString *className = presentedViewController ? NSStringFromClass([presentedViewController class]) : nil;
if ([className isEqualToString:@"AVFullScreenViewController"]) {
return UIInterfaceOrientationMaskLandscape;
}
return UIInterfaceOrientationMaskPortrait;
}
This didn't changes behaviour of AVFullScreenViewController on iOS 10 and less, but fixes navigation bar on iOS 11, so there is no need to update frame (also there is a side-effect on iOS 11 that video rotates from landscape when starts playing, but it's a tradeoff). Next, we need to add check in UIWindowDidBecomeHiddenNotification
method:
这并没有改变iOS 10及更低版本的AVFullScreenViewController的行为,但修复了iOS 11上的导航栏,因此无需更新帧(iOS 11上也有一个副作用,视频在开始播放时从横向旋转,但这是一个权衡)。接下来,我们需要在UIWindowDidBecomeHiddenNotification方法中添加check:
- (void)videoDidExitFullscreen {
if (@available(iOS 11, *)) {
// Fixes status bar on iPhone X
[self setNeedsStatusBarAppearanceUpdate];
} else {
self.navigationController.navigationBar.frame = CGRectMake(0, 0, self.view.bounds.size.width, statusAndNavBarHeight);
}
}
Without setNeedsStatusBarAppearanceUpdate
text in status bar will not appear on iPhone X, for other devices it's not needed.
如果没有setNeedsStatusBarAppearanceUpdate状态栏中的文本将不会出现在iPhone X上,对于其他设备则不需要。
#8
-1
My answer on this question works great. Here It will play the video inside your WebView normally but if you tilt your phone it will play in Landscape!
我对这个问题的答案很有用。这里它会正常播放你的WebView中的视频,但如果你倾斜你的手机,它将在风景中播放!
Also important to note is if you include youtube.com as the Base URL it will load much quicker.
另外需要注意的是,如果您将youtube.com作为基本URL包含,它将加载得更快。
Make a UIWebView in your storyboard and connect the @property to it, then reference below.
在故事板中创建一个UIWebView并将@property连接到它,然后在下面引用。
CGFloat width = self.webView.frame.size.height;
CGFloat height = self.webView.frame.size.width;
NSString *youTubeVideoCode = @"dQw4w9WgXcQ";
NSString *embedHTML = @"<iframe width=\"%f\" height=\"%f\" src=\"http://www.youtube.com/embed/%@\" frameborder=\"0\" style=\"margin:-8px;padding:0;\" allowfullscreen></iframe>";
NSString *html = [NSString stringWithFormat:embedHTML, width, height, youTubeVideoCode];
self.webView.scrollView.bounces = NO;
[self.webView loadHTMLString:html baseURL:[NSURL URLWithString:@"http://www.youtube.com"]];