在 UIWebview 中居中嵌入的 YouTube 视频 Swift
Center an embedded youtube video in a UIWebview Swift
我有一个带有 youtube 视频的 webveiw,我 embedded.It 工作正常,但问题是我希望输入它并且是 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)
感谢您对此的任何帮助。
这是我在一个应用程序中用来显示在我的 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>"
我强烈建议您使用 https://github.com/youtube/youtube-ios-player-helper,它的官方方法。试试吧,你不需要自己写 html 和 css。
示例用法
1) 在您的项目中添加 pod
pod 'youtube-ios-player-helper'
2) 在xib中添加UIView
并将其class改为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
使用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)
这对我有用:
iframe width=\"100%\" height=\"315\"
我有一个带有 youtube 视频的 webveiw,我 embedded.It 工作正常,但问题是我希望输入它并且是 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)
感谢您对此的任何帮助。
这是我在一个应用程序中用来显示在我的 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>"
我强烈建议您使用 https://github.com/youtube/youtube-ios-player-helper,它的官方方法。试试吧,你不需要自己写 html 和 css。
示例用法
1) 在您的项目中添加 pod
pod 'youtube-ios-player-helper'
2) 在xib中添加UIView
并将其class改为YTPlayerView
3) 为 YTPlayerView
@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
使用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)
这对我有用:
iframe width=\"100%\" height=\"315\"