在UIWebview Swift中居中嵌入的YouTube视频

时间:2016-06-13 07:10:14

标签: ios iphone swift youtube uiwebview

我有一个带有嵌入youtube视频的webveiw。它运行正常,除了问题之外,我希望它被输入并且是Web视图宽度的大小。现在它是在左侧,而不是扩展到另一个大小。高度很好,但我希望宽度固定。

玩家是uiview 以下是我到目前为止的情况:

    let myVideo = "https://myvideo"

    let myHTML = "<iframe width=\"\(player.frame.size.width)\" height=\"\(player.frame.size.height)\" src=\"\(myVideo)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>"


    player.allowsInlineMediaPlayback = true
    player.loadHTMLString(myHTML, baseURL: nil)
    player.frame = UIScreen.mainScreen().bounds


    player.backgroundColor = UIColor.clearColor()
    player.widthAnchor.constraintEqualToConstant(250)

感谢您提供任何帮助。

4 个答案:

答案 0 :(得分:3)

我强烈建议您使用https://github.com/youtube/youtube-ios-player-helper,这是它的官方方式。尝试一下,你不需要自己编写html和css。

样本使用

1)在项目中添加pod

pod 'youtube-ios-player-helper'  

2)在xib中添加UIView并将其类更改为YTPlayerView

3)为YTPlayerView

创建IBOutlet
@property(nonatomic, strong) IBOutlet YTPlayerView *playerView;

4)在ViewController.m

NSDictionary *playerVars = @{
                              @"playsinline"    : @1,
                              @"showinfo"       : @0,
                              @"rel"            : @0,
                              @"controls"       : @1,
                              @"origin"         : @"https://www.example.com", // this is critical
                              @"modestbranding" : @1
                            };

[self.playerView loadWithVideoId:@"Youtube Video Id" playerVars:playerVars];

有关完整文档,请访问。
https://github.com/youtube/youtube-ios-player-helper

答案 1 :(得分:1)

这是我在App中用来显示以我的UIWebView为中心的YouTube视频。

let width: CGFloat = player.frame.size.width
let height = ceil((width / 16) * 9) // Assuming that the videos aspect ratio is 16:9

let htmlString = "<div style='text-align: center;'><script type='text/javascript' src='http://www.youtube.com/iframe_api'></script><script type='text/javascript'>function onYouTubeIframeAPIReady(){ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})}function onPlayerReady(a){a.target.playVideo();}</script><iframe id='playerId' type='text/html' width='\(width)' height='\(height)' src='http://www.youtube.com/embed/\(youtubeVideoID)?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'></div>"

答案 2 :(得分:1)

使用下面的align="middle"完整代码

let myVideo = "https://myvideo"

    let myHTML = "<iframe align=\"middle\" width=\"\(player.frame.size.width)\" height=\"\(player.frame.size.height)\" src=\"\(myVideo)?&playsinline=1\" frameborder=\"0\" allowfullscreen></iframe>"


    player.allowsInlineMediaPlayback = true
    player.loadHTMLString(myHTML, baseURL: nil)
    player.frame = UIScreen.mainScreen().bounds


    player.backgroundColor = UIColor.clearColor()
    player.widthAnchor.constraintEqualToConstant(250)

答案 3 :(得分:1)

这对我有用:

<?php 
if(isset($_POST['submitButton'])){

    function errorMessage($error) {
        echo 'Apologies but the request has not been successful<br/>';
        echo 'Please see below, amend and then resubmit<br/><ul>';
        echo $error . '</ul>';
        die();
    }
    $error_message = ""; // set the error message as empty

    $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/'; // string to look for
    $string_exp = "/^[A-Za-z .'-]+$/"; // string to look for
    $contact_exp = '/^[0-9]/';


    $subject = " Request"; // subject of their email to me
    $subject2 = "Copy of your Request"; // subject of the email back to them
    $to = "myemail"; // this is my Email address
    $from = $_POST['emailAddressField']; // this is the requesters Email address

    if (!preg_match($email_exp,$from)) {    
        $error_message . = '<li>The Email Address you entered does not appear to be valid.</li>';
    }

    $first_name = $_POST['firstNameField']; // this is their first name

    if (!preg_match($string_exp,$first_name)) {
        $error_message . = '<li>The first name you entered does not appear to be valid.</li>';
    }

    $last_name = $_POST['surnameField']; // this is their surname

    if (!preg_match($string_exp,$last_name)) {
        $error_message . = '<li>The surname you entered does not appear to be valid.</li>';
    }

    $contact_number = $_POST['contactNumberField']; // this is their contact number

    if (!preg_match($contact_exp,$contact_number)){
        $error_message . = '<li>The contact number you entered does not appear to be valid.</li>';
    }

    $details_field = $_POST['detailsField']; // this is the details

    if (strlen($details_field) < 10) {
        $error_message . = '<li>Please give more details, at least 10 characters.</li>';
    }

    $message = $first_name . " " . $last_name . " (" . $contact_number . ") wrote the following:" . "\n\n" . $details_field; // message of email to me
    $message2 = "Here is a copy of your request " . $first_name . " (" . $contact_number . ").\n\n" . $details_field . "\n\nMany thanks"; // message of email to requester

    $headers = "From:" . $from; // message of email header to me
    $headers2 = "From:" . $to; // message of email header to requester

    // if there have been errors, then display error message and end
    if(strlen($error_message) > 0) {
        errorMessage($error_message);
    }

    // send the emails
    mail($to,$subject,$message,$headers);
    mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
    echo "Request sent. Thank you " . $first_name . ", I will contact you shortly.";

}
?>