UIView animated changes with auto layout constraints

前端 未结 3 1615
青春惊慌失措
青春惊慌失措 2020-12-23 14:29

Say that I have a project which looks like the following:

\"enter

There are tw

相关标签:
3条回答
  • 2020-12-23 15:02
    //
    //  WPViewController.h
    //  AutoLayoutTEst
    //
    //  Created by VASANTH K on 09/01/14.
    //  
    //
    
    #import <UIKit/UIKit.h>
    
    @interface WPViewController : UIViewController
    @property (strong, nonatomic) IBOutlet UIButton *button1;
    - (IBAction)buttClicked:(id)sender;
    @property (strong, nonatomic) IBOutlet NSLayoutConstraint *heightConstrain;
    
    @property (strong, nonatomic) IBOutlet UIButton *button2;
    @end
    

    click Event

    - (IBAction)buttClicked:(id)sender {
    
    
        self.heightConstrain.constant=100;
    
    
    }
    

    here u need to set the heightConstrain for the Yellow button then create reference outlet for that button then update the heightConstrain to update the size of that button it will automatically move your Red button down.

    https://github.com/vasanth3008/AutoLayoutHeighDemo

    refer my sample demo project

    0 讨论(0)
  • 2020-12-23 15:12

    Try it as mentioned by shwet-solanki, but add the following line after changing the constraint:

    [self.view layoutIfNeeded];
    

    the IBAction would look like:

    - (IBAction)expandYellowBox:(id)sender {
    
    [UIView animateWithDuration:0.5
                     animations:^{
                         self.yellowBoxHeightConstraint.constant += 50;
                         [self.view layoutIfNeeded];
                     }];
    }
    

    Where yellowBoxHeightConstraint is the IBOutlet for height constraint of yellowBox. Hope this helps.

    0 讨论(0)
  • 2020-12-23 15:26
    1. Add Height constraint to both the views
    2. Create an IBOutlet for height constraint of yellowbox
    3. Now instead of changing the height of yellowbox in the button pressed event, rather change the value of the height constraint. i.e suppose your IBOutlet constraint name is yellowBoxHeightConstraint, then yellowBoxHeightConstraint.constant += 50;

    Hope this works for you.

    0 讨论(0)
提交回复
热议问题