【问题标题】:unsatisfied constraints appear after adding a AVPlayerViewController Programatically以编程方式添加 AVPlayerViewController 后出现不满足的约束
【发布时间】:2016-08-23 11:12:07
【问题描述】:

我一直在像这样以编程方式添加AVPlayerViewcontroller

     playerViewController = [[AVPlayerViewController alloc]init];
     playerViewController.view.frame = CGRectMake(40,60, 500, 700);
     playerViewController.showsPlaybackControls=YES;
     [self.view addSubview:playerViewController.view];

我收到了很多限制警告

> <NSAutoresizingMaskLayoutConstraint:0x14defa560 h=-&- v=-&- _UIBackdropContentView:0x14deae530.width == _UIBackdropView:0x14deab370.width>",
"<NSLayoutConstraint:0x14dee1b70 H:|-(14)-[UILabel:0x14dee0ea0'Hi-Speed Scrubbing']   (Names: '|':_UIBackdropContentView:0x14deae530 )>",
"<NSLayoutConstraint:0x14dee1bf0 H:[UILabel:0x14dee0ea0'Hi-Speed Scrubbing']-(14)-|   (Names: '|':_UIBackdropContentView:0x14deae530 )>",
"<NSLayoutConstraint:0x14dee1670 H:|-(0)-[_UIBackdropView:0x14deab370]   (Names: '|':UIView:0x14deab090 )>",
"<NSLayoutConstraint:0x14dee16f0 H:[_UIBackdropView:0x14deab370]-(0)-|   (Names: '|':UIView:0x14deab090 )>",
"<NSLayoutConstraint:0x14dee1330 H:|-(0)-[UIView:0x14deab090]   (Names: '|':AVAlphaUpdatingView:0x14deaaad0 )>",
"<NSLayoutConstraint:0x14dee13b0 H:[UIView:0x14deab090]-(0)-|   (Names: '|':AVAlphaUpdatingView:0x14deaaad0 )>",
"<NSLayoutConstraint:0x14defb080 'UIView-Encapsulated-Layout-Width' H:[AVAlphaUpdatingView:0x14deaaad0(0)]>"

我也尝试过给予

playerViewController.view.translatesAutoresizingMaskIntoConstraints = NO

我仍然遇到约束问题。谁能给我一个解决方案?

【问题讨论】:

  • 您确定只有在将 playerViewController 添加到视图层次结构后才会收到约束警告。

标签: ios objective-c nslayoutconstraint ios-autolayout avplayerviewcontroller


【解决方案1】:

在你的 playerViewController 上使用自动布局。

【讨论】:

    【解决方案2】:

    我认为你不应该设置playerviewcontroller 的框架。你可以像这样管理它,

     NSURL *videoURL = [NSURL URLWithString:@"url string here"];
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerViewController *playerViewController = [AVPlayerViewController new];
    playerViewController.player = player;
    [self presentViewController:playerViewController animated:YES completion:nil];
    

    或者您可以将playerViewController's view 添加到self.view 而不是像这样呈现,

     [self.view addSubview:playerViewController.view];
    

    更新:

    试试这个,

     NSURL *videoURL = [NSURL URLWithString:@"url string"];
    AVPlayer *player = [AVPlayer playerWithURL:videoURL];
    AVPlayerLayer *playerLayer = [AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame = self.view.bounds;
    [self.view.layer addSublayer:playerLayer];
    [player play];
    

    并确保如果您有其他控件,例如 labelbutton,那么您已经为此设置了适当的约束。

    【讨论】:

    • 我需要将 playerviewconrtoller 作为视图 ..添加为子视图时我遇到了约束问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    相关资源
    最近更新 更多