gpt4 book ai didi

ios - 如何为视频播放器设置自动布局约束

转载 作者:行者123 更新时间:2023-12-01 18:10:47 25 4
gpt4 key购买 nike

我在 View Controller 中播放视频,我想设置视频播放器的顶部空间、前导空间、尾随空间。如何以编程方式使用自动布局进行设置?

这是我显示电影播放器​​的代码。

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",[WebArray objectForKey:@"video_url"]]];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerLayer *layer = [AVPlayerLayer layer];
[layer setPlayer:player];
[layer setFrame:CGRectMake(20, 86, 280, 160)];
[layer setBackgroundColor:[UIColor redColor].CGColor];
[layer setVideoGravity:AVLayerVideoGravityResizeAspectFill];

[self.view.layer addSublayer:layer];
[player play];

请帮我。提前致谢 !!!!

最佳答案

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-(5)-[layer(==20)]-(5)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(layer)]];

[self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-(5)-[layer(==20)]-(0)-|" options:NSLayoutFormatDirectionLeadingToTrailing metrics:nil views:NSDictionaryOfVariableBindings(layer)]];

这里 5 是前导、顶部、尾随空间,20 是高度和宽度

编辑1:
首先将 AVPlayer 添加到新的 UIView,然后将约束添加到该 View
UIView *myview = [UIView alloc] init];
[myview.layer addSublayer:layer];

关于ios - 如何为视频播放器设置自动布局约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31962603/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com