IOS关于故事板中UIWebView的问题

时间:2013-10-18 15:29:56

标签: ios xcode video uiwebview storyboard

嘿所以我已经做了大量研究,将UIWebView用作YouTube视频的视频播放器。目前我有一个工作解决方案,它使用故事板中的UIWebView设置来播放YouTube视频。 UIWebView将视频显示为预览,当点击视频在标准IOS视频播放器中打开时,视频完成后用户点击完成视频关闭,控制权返回给应用程序。 Web视图通过IBOutlet链接,设置如下:

ViewController.h:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic, weak) IBOutlet UIWebView *mainWebView;

- (void)embedYouTube:(NSString *)url coorperateVideo:(UIWebView *)videoWebView;

@end

ViewController.m:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

@synthesize mainWebView = _mainWebView;

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSLog(@"Video Frame Width: %f", _mainWebView.frame.size.width);
    NSLog(@"Video Frame Height: %f", _mainWebView.frame.size.height);

    [self embedYouTube:@"http://www.youtube.com/" coorperateVideo:_mainWebView];
    // Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)embedYouTube:(NSString *)url coorperateVideo:(UIWebView *)videoWebView
{
    NSLog(@"Video Frame Width: %f", videoWebView.frame.size.width);
    NSLog(@"Video Frame Height: %f", videoWebView.frame.size.height);

    NSInteger width = 280;
    NSInteger height = 170;

    videoWebView.allowsInlineMediaPlayback = YES;
    videoWebView.mediaPlaybackRequiresUserAction = NO;
    videoWebView.mediaPlaybackAllowsAirPlay = YES;
    videoWebView.delegate = self;
    videoWebView.scrollView.bounces = NO;
    [videoWebView.scrollView setScrollEnabled:NO];

    NSString *linkObj = @"http://www.youtube.com/v/1iBIcJFRLBA";      //@"http://www.youtube.com/v/6MaSTM769Gk";

    NSString *embedHTML = [NSString stringWithFormat:@"\
    <html><head>\
    <style type=\"text/css\">\
    body {\
    background-color: transparent;color: white;}\\</style>\\</head><body style=\"margin:0\">\\<embed webkit-playsinline id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \\width=\"%d\" height=\"%d\"></embed>\\</body></html>", @"%@", width, height];

    NSString *html = [NSString stringWithFormat:embedHTML, linkObj];
    [videoWebView loadHTMLString:html baseURL:nil];
}

@end

我有两个关于网页视图设置和处理视频播放器行为的问题。

1)由于某些原因,即使在故事板视图控制器中创建UIWebView,Web视图也不会通过框架启动。对于withs和height,viewDidLoadcoorperateVideo方法中的NSLog都返回0.0。为什么是这样?我的印象是,在故事板中创建的大多数视图都是使用大小检查器中指定的框架启动的。在使用之前是否需要设置Web视图的框架?

2)我的第二个问题与按下视频时调用的视频播放器有关。目前,应用程序已设置为唯一支持的界面方向为纵向。但是,当我点击视频并且播放器显示时,用户仅限于以横向方式观看视频。有没有办法设置它,以便只有视频播放器视图可以支持其他方向?

感谢您的帮助!

2 个答案:

答案 0 :(得分:0)

要回答您的第一个问题,我会复制/粘贴您的代码并尝试使用它。它的工作正常。两种方法都显示了Web视图的框架。如果将UIWebview拖放到情节提要板,则高度和宽度将显示在大小检查器中。如果你愿意,你可以明确提及它。

答案 1 :(得分:0)

相同的代码我跑了,发现工作完美。如上所述,做一件事从故事板中删除UIWebview。然后为它提供适当的约束。它会起作用......干杯......