以编程方式和故事板进行约束

Constraints programmatically and with Storyboards

可以在使用情节提要时以编程方式设置约束吗?我正在使用这个 https://github.com/raphaelschaad/RSPlayPauseButton as well as this for constraints https://github.com/SnapKit/Masonry。但是我没有正确显示它们:

左边的图片显示不正确,而且不可点击(它是一个按钮)。

相关代码:

- (void)viewDidLoad {

    [super viewDidLoad];

    _playPauseButton = [[RSPlayPauseButton alloc] init];
    _playPauseButton.tintColor = [UIColor blackColor];
    [_playPauseButton addTarget:self action:@selector(playPauseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playPauseButton];
}

- (void)viewDidLayoutSubviews
{
    [self.playPauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.view).with.offset(10);
        }];
}

您需要为 playPauseButton 添加 width/height 约束,试试这个:

- (void)viewDidLoad {
    [super viewDidLoad];

    _playPauseButton = [[RSPlayPauseButton alloc] init];
    _playPauseButton.tintColor = [UIColor blackColor];
    [_playPauseButton addTarget:self action:@selector(playPauseButtonDidPress:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_playPauseButton];

    [playPauseButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self.view).with.offset(10);
        make.width.equalTo(@(50));
        make.height.equalTo(@(50));
    }];
}