Search code examples
objective-cxcode-storyboardmasonry-ios-osx

Constraints programmatically and with Storyboards


It is possible to set constraints programmatically and while using Storyboards? I am using this https://github.com/raphaelschaad/RSPlayPauseButton as well as this for constraints https://github.com/SnapKit/Masonry. However I they are not being displayed correctly:
Bad Good

The image on the left is not being displayed correctly, and it not clickable (it is a button).

Relevant code:

- (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);
        }];
}

Solution

  • You need to adding width/height constraints for the playPauseButton, try this:

    - (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));
        }];
    }