0

I have a button and on click event of it i want to insert a UIView. I know how to insert a UIView however i am not able to push/move other UIView's that are below it so that i can accommodate the added UIView.

I have already looked at this link but it does not serve my purpose

Any suggestions?

Community
  • 1
  • 1
Anish
  • 745
  • 2
  • 8
  • 21
  • It depends. Are you using auto-layout, or struts-and-springs? The answer is quite different for those different approaches to form layout. – Duncan C Jan 17 '14 at 13:56
  • I will be inserting the UIView programmatically on button click event – Anish Jan 17 '14 at 14:01
  • That's not what I asked. Are you using auto-layout or struts and springs. The answer to how to do this is totally different depending on your answer. – Duncan C Jan 17 '14 at 14:24

1 Answers1

1

In the method of the button create the new view and add it as a subview. Set the frame.origin.x to 0 minus the width of the view (if you want it to push from the left) so it is not visible and change that value to where you want it to be inside [UIView animateWithDuration:animations:].

[UIView animateWithDuration:1.0 animations:^(void){
    [newView setFrame:({
        CGRect frame = newView.frame;
        frame.origin.x = 150; // If you want it to start at x = 150
        frame;
    })];

    // If you want to move other views too, just add them in this block
}];

If you don't get what I'm doing to set the frame, check out the first tip in this post.

yoeriboven
  • 3,541
  • 3
  • 25
  • 40
  • I want to push the relative views downward that are already present if the new view gets inserted. – Anish Jan 17 '14 at 13:58
  • 1
    You can just add them in the block. Just try this code out and let me know if it worked. – yoeriboven Jan 17 '14 at 14:00
  • That code will not work if the OP is using auto-layout. You have to manipulate constraints rather than moving object's frames/centers. Also, even if you're not using auto-layout, it's better to manipulate a view's center property than it's frame, since the frame property doesn't work if the view has a modified transform. – Duncan C Jan 17 '14 at 14:37
  • @yoeriboven this works perfectly fine with auto layout off. It doesn't seem to work when auto layout is on. – Anish Feb 07 '14 at 13:11