4

I am very new to iPhone/iPad development. can you please help me create this programmatically. I want to expand/collapse a UIView programmatically.

This expandable/collapsable view will have some text field and lables which should appear and disappear with that view

Dilip Manek
  • 9,095
  • 5
  • 44
  • 56
Ankit Sachan
  • 7,690
  • 16
  • 63
  • 98

4 Answers4

12

Suppose you have a UIView instance in the UIViewController class like this:

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(x, y, w1, h1)];
[self.view addSubview:view];

based on the requirement you set the view visibility as I did here .. I am not displaying the view when the controller is loading it's view ..

[view setHidden:YES];

Maintain a flag to check the visibility of the view instance .. lets say isViewVisible is my flag to check the visibility of the view .. I set it to NO in the begning ..

isHelpViewVisible = NO;

and I wrote an action method (viewClicked) here to expand and to collapse the view object , give this action method to a button instance and it will work.

- (void)viewClicked:(id)sender {

    if (!isViewVisible) {
        isViewVisible = YES;
        [view setHidden:NO];
        [UIView beginAnimations:@"animationOff" context:NULL]; 
        [UIView setAnimationDuration:1.3f];
        [view setFrame:CGRectMake(x, y, w1, h1)];
        [UIView commitAnimations];
    } else {
        isViewVisible = NO;
        [view setHidden:NO];
        [UIView beginAnimations:@"animationOff" context:NULL]; 
        [UIView setAnimationDuration:1.3f];
        [view setFrame:CGRectMake(x, y, width, hight)];
        [UIView commitAnimations];
    }

}

and add the textfields and labels objects to the view object as subviews and set the animation to those objects as well .. it will work.

Dee
  • 1,887
  • 19
  • 47
3

Replaces the following from above:

[UIView beginAnimations:@"animationOff" context:NULL];
[UIView setAnimationDuration:1.3f];
[view setFrame:CGRectMake(x, y, w1, h1)];
[UIView commitAnimations];

With the New way of doing animations:

[UIView animateWithDuration:1.3f animations:^{
    self.frame = CGRectMake( x1, x2, w1, h1);
} completion:^(BOOL finished) {
    // this gives us a nice callback when it finishes the animation :)
}];
Danoli3
  • 3,203
  • 3
  • 24
  • 35
1

To change the size / position of a UIView within its parent just change its frame property:

CGRect newFrame = myView.frame;
newFrame.origin.x = newX;
newFrame.origin.y = newY;
newFrame.size.width = newWidth;
newFrame.size.height = newHeight;
myView.frame = newFrame;
Bogatyr
  • 19,255
  • 7
  • 59
  • 72
0

You can try this

CGRect frame = [currentView frame];
frame.size.width = 999;//Some value
frame.size.height = 444;//some value
[currentView setFrame:frame];
You can follow this whenever you want to increase/decrease view size
SNR
  • 1,249
  • 2
  • 20
  • 38