iOS - UIScrollView de media pantalla a pantalla completa

En una UIViewController, Tengo un UIScrollView that takes half of the screen. This UIScrollView contiene una colección de UIView. On some specific event, such as a swipe, I want my UIScrollView to take full screen animatedly, how do I achieve this behavior?

preguntado el 12 de febrero de 14 a las 08:02

4 Respuestas

Prueba esto ...

// adding swipe gesture to your scrollview
UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

// Set swipe direction.
[swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[scrollview addGestureRecognizer:swipeLeft];

// add gesture action method
- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe {
// if you are having only one swipe gesture, you no need to add the below condition. you can add the code inside the condition
if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
    NSLog(@"Left Swipe");
    scrollView.contentSize = self.view.frame;
    scrollView.frame = self.view.frame;

Respondido 12 Feb 14, 08:02

not working, my UIScrollView doesn't change its size at all - bohanl

can i know how you set the scrollview. are you using xib or programmatically. if possible can you share snippet of your code - RAJA

same code its working for me.. i am able to re-size scrollview on swipe.. can you please check it. - RAJA

I was just trying to test so I added the code in viewDidLoad, which for some reason didn't work. But if I tried to add to the event like handleSwipe it worked. But is there a way to do this animatedly? - bohanl

I think I figured, I used [UIView beginbeginAnimations:context:] y [UIView commitAnimations] - bohanl

Try using this method on event and set :

scrollView.contentSize = CGSizeMake(847, 800); // change on basis of your requirement

For animating scroll view :

- (void)setContentOffset:(CGPoint)contentOffset animated:(BOOL)animated;  // animate at constant velocity to new offset

Respondido 12 Feb 14, 08:02

setting content offset won't change the size of the UIScrollView - bohanl

Yes ..if you see carefully I have mentioned contentSize for setting and for animation I have said contentOffset - Samkit jainista

Quoting from @RAJA's answer and slight improvement:

[UIView beginAnimations:@"whatever" context:nil];

scrollView.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * <count of subviews>);
scrollView.frame = self.view.frame;

[UIView commitAnimations];

Respondido 12 Feb 14, 09:02

Please set contentsize as follows.. scrollview.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height * <count of subviews>); - RAJA

I just extend the first answer, If you want animate the above process, you can achieve that by appending the following code :

- (void)handleSwipe:(UISwipeGestureRecognizer *)swipe 
   if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) 
    [UIView animateWithDuration:0.15f  // set duration for your animation here

                         scrollView.contentSize = self.view.frame;
                         scrollView.frame = self.view.frame;
                     completion:^(BOOL finished){
                         NSLog(@"completion block");


Respondido 12 Feb 14, 10:02

I was able to do the same using NSLayoutConstraint just by changing its height using NSLayoutAttributeHeight. I know this solution might not be typical but do you see any drawbacks? - bohanl

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.