Frage

I have two view controllers inside a tab bar navigation. Inside the second scene I have an additional view (just a simple UIView) and a button to set it's color and bounds.

CGRect viewRect = CGRectMake(20, 20, 70, 70);
self.animationView.bounds = viewRect;

self.animationView.backgroundColor =
[UIColor yellowColor];

This code works fine. But if I navigate to the first view controller and then back to the second view controller my view is still yellow but it is back at the size and position I set in interface builder.

How can I prevent this? This behavior ends if I disable autolayout but I don't really want to do that.

War es hilfreich?

Lösung

Create outlets for the animationView constraints, and change their constant value.

In the .h file of the viewcontroller: Connect the outlets to the correct constraint in the IB:

    //AnimationView Height Constraint 
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *cHeight;
    //AnimationView Width Constraint 
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *cWidth;
    //AnimationView Leading Constraint 
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *cLeading;
    //AnimationView Top Constraint 
    @property (weak, nonatomic) IBOutlet NSLayoutConstraint *cTop;

In the .m file of the ViewController set the constant value of the constraints, instead of the frame:

    - (IBAction)btnTouched:(id)sender {
        [_cHeight setConstant:70];
        [_cWidth setConstant:70];
        [_cTop setConstant:20];
        [_cLeading setConstant:20];

        self.animationView.backgroundColor = [UIColor yellowColor];
    }

It'll work fine.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top