wangii
wangii

Reputation: 2940

Autolayout stops UITapGestureRecognizer?

I setup a UIImageView to accept tap gesture, which is tapped, the button shall go hidden and shows a UIActivityIndicatorView at the same position. The logic is very simple. but I have two problems:

code:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Do any additional setup after loading the view.

    // setup the loading activity indicator;
    loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [self.view addSubview:loading];
    loading.hidden = YES;

    // setup the fb button
    fb = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"facebook.png"]];
    fb.userInteractionEnabled = YES;

    UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc] init];
    [tap addTarget:self action:@selector(login:)];
    [fb addGestureRecognizer:tap];

    [self.view addSubview:fb];
    fb.hidden = YES;

    NSLayoutConstraint *c;
    c = [NSLayoutConstraint constraintWithItem:fb
                                     attribute:NSLayoutAttributeCenterX
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeCenterX
                                    multiplier:1
                                      constant:0];
    [self.view addConstraint:c];
    c = [NSLayoutConstraint constraintWithItem:fb
                                     attribute:NSLayoutAttributeCenterY
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeCenterY
                                    multiplier:1
                                      constant:0];
    [self.view addConstraint:c];

    c = [NSLayoutConstraint constraintWithItem:loading
                                     attribute:NSLayoutAttributeCenterX
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeCenterX
                                    multiplier:1
                                      constant:0];
    [self.view addConstraint:c];
    c = [NSLayoutConstraint constraintWithItem:loading
                                     attribute:NSLayoutAttributeCenterY
                                     relatedBy:NSLayoutRelationEqual
                                        toItem:self.view
                                     attribute:NSLayoutAttributeCenterY
                                    multiplier:1
                                      constant:0];
    [self.view addConstraint:c];

    [fb setTranslatesAutoresizingMaskIntoConstraints:NO];
    [loading setTranslatesAutoresizingMaskIntoConstraints:NO];
    [self.view setTranslatesAutoresizingMaskIntoConstraints:NO];
}

Upvotes: 2

Views: 969

Answers (1)

Cristi Habliuc
Cristi Habliuc

Reputation: 171

You need to call setTranslatesAutoresizingMaskIntoConstraints:NO because the autoresizing mask gets converted to constraints which conflict with the autolayout system. You usually cannot add auto layout constraints to views with autoresizing masks constraints or you'll get into incompatible constraints.

The main issue in your case is that the width and height of those issues cannot be determined. You need to add width and height constraints as well. The layout can't be determined using the centering constraints if the auto layout system can't calculate them.

Upvotes: 2

Related Questions