我有一个嵌入了 youtube 视频的 webveiw。它工作正常,但问题是我希望输入它并使其成为 web View 宽度的大小。现在它在左侧,没有扩展到其他尺寸。高度很好,但我希望宽度固定。
player 是 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)
感谢您对此的任何帮助。
最佳答案
我强烈建议您使用 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
关于ios - 在 UIWebview Swift 中居中嵌入的 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37783712/