尝试将按钮添加为子视图时出现约束错误

时间:2020-07-29 16:37:32

标签: ios objective-c xcode

我正在尝试在清理栏上方屏幕左下角的AVPlayerViewController(对于我的tvOS应用程序)中放置一个简单按钮。我收到一条错误消息,指出“约束项必须分别是视图或布局指南”。

我没有正确设置定位,因为我只是想看看我是否可以完全显示该按钮。有人可以告知为什么此代码崩溃吗?

- (void)setupBreakButton
{
    NSLog(@"**** In setupBreakButton....");
    UIButton* skipBreakButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    skipBreakButton.backgroundColor = [[UIColor bm_colorWithHexString:@"64A6BD"] colorWithAlphaComponent:1.0f];
    skipBreakButton.layer.cornerRadius = 6;
    
    skipBreakButton.translatesAutoresizingMaskIntoConstraints = NO;
    /* Leading space to superview */
    NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
                                                 constraintWithItem:skipBreakButton.leadingAnchor attribute:NSLayoutAttributeLeft
                                                 relatedBy:NSLayoutRelationEqual toItem:self attribute:
                                                 NSLayoutAttributeLeft multiplier:1.0 constant:30];
    /* Top space to superview Y*/
    NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
                                                 constraintWithItem:skipBreakButton.topAnchor attribute:NSLayoutAttributeTop
                                                 relatedBy:NSLayoutRelationEqual toItem:self attribute:
                                                 NSLayoutAttributeTop multiplier:1.0f constant:258];
    /* Fixed width */
    NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton.widthAnchor
                                                                       attribute:NSLayoutAttributeWidth
                                                                       relatedBy:NSLayoutRelationEqual
                                                                          toItem:nil
                                                                       attribute:NSLayoutAttributeNotAnAttribute
                                                                      multiplier:1.0
                                                                        constant:35];
    /* Fixed Height */
    NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton.heightAnchor
                                                                        attribute:NSLayoutAttributeHeight
                                                                        relatedBy:NSLayoutRelationEqual
                                                                           toItem:nil
                                                                        attribute:NSLayoutAttributeNotAnAttribute
                                                                       multiplier:1.0
                                                                         constant:12];
    /* 4. Add the constraints to button's superview*/
    
    [self.view addConstraints:@[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
}

此外,如何将这个按钮添加为AVPlayerViewController的子视图?

1 个答案:

答案 0 :(得分:1)

在自定义viewController内部调用此方法

UIButton* skipBreakButton;


- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.

NSURL *url = [[NSURL alloc] initWithString:@""];

AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];

[self addChildViewController:controller];
[self.view addSubview:controller.view];

controller.view.frame = CGRectMake(0,0,UIScreen.mainScreen.bounds.size.width,UIScreen.mainScreen.bounds.size.height);
controller.player = player;
controller.showsPlaybackControls = YES;
[player play];

NSLog(@"**** In setupBreakButton....");

skipBreakButton = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
skipBreakButton.backgroundColor = [[UIColor redColor] colorWithAlphaComponent:1.0f];
skipBreakButton.layer.cornerRadius = 6;

[skipBreakButton addTarget:self action:@selector(didTapButton:) forControlEvents:UIControlEventPrimaryActionTriggered];

[self.view addSubview: skipBreakButton];

skipBreakButton.translatesAutoresizingMaskIntoConstraints = NO;
/* Leading space to superview */
NSLayoutConstraint *leftButtonXConstraint = [NSLayoutConstraint
                                             constraintWithItem:skipBreakButton attribute:NSLayoutAttributeLeading
                                             relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
                                             NSLayoutAttributeLeading multiplier:1.0 constant:30];
/* Top space to superview Y*/
NSLayoutConstraint *leftButtonYConstraint = [NSLayoutConstraint
                                             constraintWithItem:skipBreakButton attribute:NSLayoutAttributeTop
                                             relatedBy:NSLayoutRelationEqual toItem:self.view attribute:
                                             NSLayoutAttributeTop multiplier:1.0f constant:258];
/* Fixed width */
NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton
                                                                   attribute:NSLayoutAttributeWidth
                                                                   relatedBy:NSLayoutRelationEqual
                                                                      toItem:nil
                                                                   attribute:NSLayoutAttributeNotAnAttribute
                                                                  multiplier:1.0
                                                                    constant:35];
/* Fixed Height */
NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:skipBreakButton
                                                                    attribute:NSLayoutAttributeHeight
                                                                    relatedBy:NSLayoutRelationEqual
                                                                       toItem:nil
                                                                    attribute:NSLayoutAttributeNotAnAttribute
                                                                   multiplier:1.0
                                                                     constant:12];
/* 4. Add the constraints to button's superview*/

[self.view addConstraints:@[leftButtonXConstraint, leftButtonYConstraint, widthConstraint, heightConstraint]];
}

- (NSArray<id<UIFocusEnvironment>> *)preferredFocusEnvironments {

return @[skipBreakButton];
}

-(void)didTapButton:(UIButton *) sender {
NSLog(@"didtapbutton");
}
相关问题