Search code examples
iosobjective-cswiftautolayout

Stop UILabel text change from triggering auto layout


I have a ViewController's view that is initialized with AutoLayout. As part of this view, I also have an UIImageView with some constraints - 0 top, 0 left, 0 right, 300 bottom

At some point, I execute a range of animations of which some zoom in and change the center of the image

self.imageView.center = CGPointMake(w*s/widthDivider, h-h*s/heightDivider);

Everything looks fine up until I have to change some UILabel's text on the page, and as I've noticed it will trigger AutoLayout which puts back my zoomed image into its initial constraints and break my layout.

self.label.text = [NSString stringWithFormat:@"%d", 23];

Is there any way in which I can stop the AutoLayout trigger when changing the above label's text?

Refactoring the animations which center the image to be also based on constraints would be a solution that I have to take at a certain point if I cannot stop the AutoLayout trigger on text change.


Solution

  • EDIT

    I added the subclass idea. Note I was able to prevent autolayout to trigger with all of these approaches.

    Using constraints

    I think it is a design issue and you'll need to do that refactoring. Below just some ideas to try. Not really preventing autolayout but trying to prevent the label from triggering it.

    Once the animation is done, what if you remove or deactivate all the label's constraints. So do something like

    // Remove all constraints
    [label removeConstraints:label.constraints];
    

    or

    // Deactivate constraints
    for ( NSLayoutConstraint * c in label.constraints )
    {
        c.active = NO;
    }
    

    These will prevent the constraints from updating and that might prevent autolayout from triggering altogether.

    Subclassing

    I added this later. It also prevents autolayout but again your situation seems different. Anyhow, how about this. Subclass UILabel and override its invalidateIntrinsicContentSize message as below. The self.stopLayout is a BOOL that I set to YES when I no longer want to trigger autolayout and it works nicely on this side.

    - ( void ) invalidateIntrinsicContentSize
    {
        if ( ! self.stopLayout )
        {
            super.invalidateIntrinsicContentSize;
        }
    }