Я действительно застрял в этой проблеме. Я пытаюсь добавить кнопку поверх AVPlayerViewController с надписью «Пропустить вступление» (аналогично кнопке, которую Netflix имеет в своем содержимом).
Я добавил 4 или 5 кнопок по горизонтали, начиная чуть выше левая сторона панели поиска и каждая кнопка разнесены примерно на 10 пунктов. Мне нужна только одна кнопка, но на данный момент я не могу понять, почему я вообще не могу нажать ни одну из этих кнопок:
![enter image description here](https://i.stack.imgur.com/I63VZ.jpg)
I've declared the button and the focus guide variables as private:
введите описание изображения здесь
Вот метод viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
self.avPlayerViewController = [[AVPlayerViewController alloc] init];
[self addChildViewController:self.avPlayerViewController];
[self.view addSubview:self.avPlayerViewController.view];
self.avPlayerViewController.showsPlaybackControls = YES;
self.avPlayerViewController.view.translatesAutoresizingMaskIntoConstraints = NO;
NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:self.avPlayerViewController.view
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeHeight
multiplier:1.0
constant:0];
NSLayoutConstraint *width = [NSLayoutConstraint constraintWithItem:self.avPlayerViewController.view
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeWidth
multiplier:1.0
constant:0.0];
NSLayoutConstraint *top = [NSLayoutConstraint constraintWithItem:self.avPlayerViewController.view
attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeTop
multiplier:1.0
constant:0.0];
NSLayoutConstraint *leading = [NSLayoutConstraint constraintWithItem:self.avPlayerViewController.view
attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual
toItem:self.view
attribute:NSLayoutAttributeLeading
multiplier:1.0
constant:0.0];
[self.view addConstraint:height];
[self.view addConstraint:width];
[self.view addConstraint:top];
[self.view addConstraint:leading];
[self.avPlayerViewController didMoveToParentViewController:self];
[self setupBreakButtonWithWidth:225 height:50 xCoordinate:100 yCoordinate:850];
[self setupBreakButtonWithWidth:225 height:50 xCoordinate:335 yCoordinate:850];
[self setupBreakButtonWithWidth:225 height:50 xCoordinate:570 yCoordinate:850];
[self setupBreakButtonWithWidth:225 height:50 xCoordinate:805 yCoordinate:850];
[self setupFocusGuide];
[self createBannerAdView];
[self resetUpNextState];
}
Как видите, я просто добавляю кнопки в нижнюю часть экрана (над полосой поиска), а затем настраиваю руководство по фокусу и предпочтительные средыFocusEnvironments:
- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {
NSLog(@"**** BMNativeVideoPlayerViewController -> preferredFocusEnvironments");
return @[self.skipBreakButton];
}
- (void)setupFocusGuide
{
NSLog(@"**** BMNativeVideoPlayerViewController -> setupFocusGuide");
// allocate focus guide and add it to the view
self.focusGuide = [[UIFocusGuide alloc] init];
[self.view addLayoutGuide:self.focusGuide];
// define constraints
[self.focusGuide.leftAnchor constraintEqualToAnchor:self.view.leftAnchor].active = YES;
[self.focusGuide.rightAnchor constraintEqualToAnchor:self.view.rightAnchor].active = YES;
[self.focusGuide.topAnchor constraintEqualToAnchor:_skipBreakButton.topAnchor].active = YES;
[self.focusGuide.bottomAnchor constraintEqualToAnchor:_skipBreakButton.bottomAnchor].active = YES;
// select the default focusable view
self.focusGuide.preferredFocusEnvironments = @[self.skipBreakButton];
}
Вот метод, в котором настраивается кнопка, а также метод обработчика кнопки и метод didUpdateFocusInContext (хотя я не знаю, делает ли он то, что должен ):
- (void)didUpdateFocusInContext:(UIFocusUpdateContext *)context withAnimationCoordinator:(UIFocusAnimationCoordinator *)coordinator
{
self.focusGuide.preferredFocusEnvironments = @[self.skipBreakButton];
}
- (void)setupBreakButtonWithWidth:(CGFloat)width height:(CGFloat)height xCoordinate:(CGFloat)x yCoordinate:(CGFloat)y
{
NSLog(@"**** BMNativeVideoPlayerViewController -> setupBreakButton");
_skipBreakButton = [[UIButton alloc] init];
_skipBreakButton.backgroundColor = [UIColor whiteColor];
[self.skipBreakButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
_skipBreakButton.alpha = 1.0f;
_skipBreakButton.layer.cornerRadius = 3;
_skipBreakButton.translatesAutoresizingMaskIntoConstraints = NO;
[self.avPlayerViewController.contentOverlayView addSubview:_skipBreakButton];
[self.avPlayerViewController.contentOverlayView bringSubviewToFront:_skipBreakButton];
NSLayoutConstraint *xConstraint = [NSLayoutConstraint
constraintWithItem:_skipBreakButton attribute:NSLayoutAttributeLeading
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeLeading multiplier:1.0 constant:x];
NSLayoutConstraint *yConstraint = [NSLayoutConstraint
constraintWithItem:_skipBreakButton attribute:NSLayoutAttributeTop
relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
NSLayoutAttributeTop multiplier:1.0f constant:y];
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:_skipBreakButton
attribute:NSLayoutAttributeWidth
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:width];
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:_skipBreakButton
attribute:NSLayoutAttributeHeight
relatedBy:NSLayoutRelationEqual
toItem:nil
attribute:NSLayoutAttributeNotAnAttribute
multiplier:1.0
constant:height];
[self.view addConstraints:@[xConstraint, yConstraint, widthConstraint, heightConstraint]];
[self.skipBreakButton setUserInteractionEnabled:YES];
[self.skipBreakButton setTitle:@"Skip Intro" forState:UIControlStateNormal];
[self.skipBreakButton.titleLabel setFont:[UIFont fontWithName:@"Helvetica-Bold" size:26.0]];
NSLog(@"**** Can the skip break button be focused? %d", self.skipBreakButton.canBecomeFocused);
[self.skipBreakButton addTarget:self action:@selector(skipBreakButtonClicked:) forControlEvents:UIControlEventPrimaryActionTriggered];
}
- (void)skipBreakButtonClicked:(UIButton *) sender {
NSLog(@"**** BMNativeVideoPlayerViewController -> skipBreakButtonClicked");
}
Я не могу нажать ни на одну из этих кнопок. Мне нужна помощь, чтобы понять, как заставить его работать.
Кажется, над этой кнопкой отображается вид, но я не уверен, как это исправить:
(lldb) po [UIFocusDebugger checkFocusabilityForItem: 0x7ff86fe60510]
The following issues were found that would prevent this item from being focusable:
- ISSUE: One or more ancestors have issues that may be preventing this item from being focusable. Details:
<_AVPlayerViewControllerContainerView 0x7ff86fc29eb0>:
- ISSUE: This view returns YES from -canBecomeFocused, which will prevent its subviews from being focusable.
Посоветуйте, пожалуйста?