How To shift a view with animation with auto layout constraints are on iOS

折月煮酒 提交于 2020-01-06 19:36:36

问题


I am doing a task in which, i need to shift the view by 35 pixels in order to show a view which is present below that view.

Or you can say i need to insert a bar like notification on top and shift the existing view by 35 pixels at runtime.

The problem is with auto layout on in order to support all the screens. Shifting is not happening as we want.... Any suggestions here ? Top view has a constraint that its distance with top view is zero, which needs to change at runtime, when we to show the below view.

Here are the screens before shifting :

After Shifting and showing the image :

I have given sidebar view as well. NotifView : yellow view that we need to show at runtime twoView : that we need to shift to display notifview on top.

-(void)shiftTwoView:(NSString*)summary{

CGFloat shiftHeight = 35.0f;

CGRect twoRect = twoView.frame;
    twoRect.origin.y += shiftHeight;
    twoRect.size.height -= shiftHeight;


[UIView animateWithDuration:3.5
                 animations:^{
                     twoView.frame = twoRect;
                 }];

}


回答1:


When you are using Auto Layout, you need not set frames like that. You can manipulate the constraints values programmatically to do it. As you haven't made it clear what all constraints you have applied here, i will explain what all you require to do so.

notifyview: - leading = 0, trailing = 0, top = 0, height = 0;  
twoView: - leading = 0, trailing = 0, top(to notifview) = 0; bottom = 0;

Now create an IBOutlet for the height constraint of notifyview, lets say notifyViewHeightConstraint.

Now to display the content of your notifyView, all you need to do is increase the height of it. By default this height is 0 from storyboard. You can change it like notifyViewHeightConstraint.constant = 40;. Once you are done and you need to disappear it again, you can set the constant value back to 0;

-(void)shiftTwoView:(NSString*)summary  
{  
     notifyViewHeightConstraint.constant = 40;   
    [UIView animateWithDuration:2.0 animations:^{  
            [self.view layoutIfNeeded];  
    }]; 
}


来源:https://stackoverflow.com/questions/34082458/how-to-shift-a-view-with-animation-with-auto-layout-constraints-are-on-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!