Problema de Diseño Automático de UIScrollView


Tengo un problema con autolayout(tal vez) y mi scrollview!

Mi problema

  1. Me desplazo hacia abajo Vista

2.Luego empuje a otra vista

3.Luego vuelvo y el scrollview se ve así y no soy capaz de desplazarse hasta el punto más alto.(Lo veo en el rebote del scrollview) Scrollview

¿alguien Puede ayudarme?

Author: Mathias Aichinger, 2012-09-25

9 answers

El siguiente fragmento de código en el controlador de vista que contiene también parece resolver el problema, sin depender de tamaños explícitos:

- (void)viewDidDisappear:(BOOL)animated {
  [super viewDidDisappear:animated];
  self.mainScrollView.contentOffset = CGPointZero;
}

Restablece el desplazamiento de contenido al origen, pero parece que también lo hacen las otras respuestas.

 39
Author: user1071136,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-10 21:36:25

Si todavía estás buscando una respuesta, la encontré hoy después de dos días de golpear la pared. Solo te pegaré el código, pero lo más importante es cuando cargues tu ScrollView..

    -(void)viewWillAppear:(BOOL)animated{

    [scrollView setFrame:CGRectMake(0, 0, 320, 800)];
}

-(void)viewDidAppear:(BOOL)animated
{
    [scrollView setScrollEnabled:YES];
    [scrollView setContentSize:CGSizeMake(320, 800)];

}

Todo esto se carga antes de -(void)viewDidLoad

Observe que la altura es en ambos casos 800, lo cual es crucial para resolver este problema. buena suerte con tu proyecto ;)

 11
Author: kadore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-02 14:10:48

Estaba usando la solución de Adam, pero empecé a tener problemas cuando estaba descartando con animación:SÍ. En mi código, el desplazamiento de contenido se establece un tiempo después de viewWillAppear (ya que viewWillAppear parece ser demasiado pronto).

- (void)viewDidDisappear:(BOOL)animated
{
    self.scrollOffsetToPersist = self.scrollView.contentOffset;
    self.scrollView.contentOffset = CGPointZero;

    [super viewDidDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    [[NSOperationQueue mainQueue] addOperationWithBlock:^
     {
         self.scrollView.contentOffset = self.scrollOffsetToPersist;
     }];
}

EDITAR: otra, mejor manera es restablecer de nuevo en viewDidLayoutSubviews:)

- (void)viewDidLayoutSubviews
{
    [super viewDidLayoutSubviews];

    if(!CGPointEqualToPoint(CGPointZero, self.scrollOffsetToPersist))
    {
        self.scrollView.contentOffset = self.scrollOffsetToPersist;
        self.scrollOffsetToPersist = CGPointZero;
    }
}
 6
Author: Kukosk,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-01-29 18:52:00

Esto no es genial, pero le gané a auto-layout (definitivamente no es la manera correcta, pero estaba harto de intentarlo!) al establecer el tamaño del contenido en viewDidAppear después de que ocurra el autodiagnóstico, establecer el scrollOffset y persistir el desplazamiento de desplazamiento en viewDidDisappear, y luego establecer el desplazamiento de nuevo a su estado persistido en viewDidAppear.

Así:

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:YES];
    self.scrollView.contentSize = self.scrollViewInnerView.frame.size;
    self.scrollView.contentOffset = [self.scrollOffsetToPersist CGPointValue];

}

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:YES];
    self.scrollOffsetToPersist = [NSValue valueWithCGPoint:self.scrollView.contentOffset];
    self.scrollView.contentOffset = CGPointZero;
}

No es nada elegante, pero funciona así que pensé en compartir.

 3
Author: Adam Waite,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-12-07 07:39:53

Utilizo un UITabBarController y diferentes vistas con auto layout. Las vistas son más largas que la pantalla del dispositivo. Cambiar de una pestaña a la otra conduce a veces al problema que describe. Esto solo sucedió si la vista se ha desplazado hacia abajo antes del cambio. Probé todos los consejos aquí, pero esto no funcionó para mi caso. Mi solución fue desplazarse hacia arriba de nuevo antes de salir de la vista. Al menos una solución para este error en iOS 6:

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [scrollView setContentOffset:CGPointZero animated:NO];
}
 1
Author: Frankenstein,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-05 22:45:11

El problema causa ScrollView se estableció contentOffset antes de que se aplicara AutoLayout. la solución es:

Crear propiedad privada

@property (assign,nonatomic) CGPoint scrollviewContentOffsetChange;

Añadir código para ver el método

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  self.scrollView.contentOffset = CGPointZero;
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  self.scrollviewContentOffsetChange = self.scrollView.contentOffset;
}

- (void)viewDidLayoutSubviews {
  [super viewDidLayoutSubviews];

  self.scrollView.contentOffset = self.scrollviewContentOffsetChange;
}
 1
Author: Ratha Hin,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-08-29 10:16:18

¿has probado esto?

self.automaticallyAdjustsScrollViewInsets = NO;

En mi caso esto fue lo que resolvió mi problema.

 1
Author: Lukas,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-04-04 12:05:06

Yo tenía el mismo problema. Resultó que estaba estableciendo una restricción en la vista de contenido alineando el centro y con el centro y de la supervisión. Cuando eliminé esta restricción funcionó bien.

 0
Author: mimc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-02-19 22:06:20

Prueba esto

@property (nonatomic, assign) CGPoint scrollViewContentOffsetChange;

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.scrollView.contentOffset = CGPointZero;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.scrollViewContentOffsetChange = _scrollView.contentOffset;
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    _scrollView.contentOffset = self.scrollViewContentOffsetChange;
}
 0
Author: Jimvanced,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-18 08:37:04