2012-10-07 12 views
9

Estoy implementando un formulario dentro de un UIScrollView. Intento agregar algo de espacio en la parte inferior del contenido de una vista de desplazamiento cuando se abre el teclado para que el usuario pueda ver todos los campos. Pongo la vista de formulario dentro de la UISCrollView añadiendo todas las limitaciones necesarias con el siguiente código:iOS: cambiar el contenido de un UIScrollview usando el diseño automático

[_infoView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:[_infoView(1730)]" options:0 metrics:nil views:views]]; 

[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_infoView]|" options:0 metrics:nil views:views]]; 

[_scrollView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_infoView]|" options:0 metrics:nil views:views]]; 

[_scrollView addConstraint:[NSLayoutConstraint constraintWithItem:_infoView 
                 attribute:NSLayoutAttributeCenterX 
                 relatedBy:NSLayoutRelationEqual 
                  toItem:_scrollView 
                 attribute:NSLayoutAttributeCenterX 
                 multiplier:1 
                 constant:0]]; 

Como se puede ver, que especifique en la primera línea de la altura de la forma y el scrollview adapta su tamaño contenido de forma automática. Ahora quiero aumentar la altura del formulario, así que he intentado restablecer la restricción de la altura con una mayor, pero no funcionará. Luego intenté usar el método [_scrollView setContentSize:], pero esto tampoco funcionará. ¿Alguien puede ayudarme por favor?

+0

¿Alguna vez resolvió esto? Me está costando muchísimo descifrar esto. – mkral

Respuesta

-2

no estoy seguro de donde se agrega el código anterior, pero el siguiente debe resolver su problema

En Su función init, agregue el siguiente:

NSNotificationCenter *center = [NSNotificationCenter defaultCenter]; 
     [center addObserver:self selector:@selector(noticeShowKeyboard:) name:UIKeyboardDidShowNotification object:nil]; 
     [center addObserver:self selector:@selector(noticeHideKeyboard:) name:UIKeyboardWillHideNotification object:nil]; 

Añadir el siguiente a su .h

CGSize keyboardSize; 
int keyboardHidden;  // 0 @ initialization, 1 if shown, 2 if hidden 

Añadir el siguiente a su .m

-(void) noticeShowKeyboard:(NSNotification *)inNotification { 
    keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; 
    keyboardHidden = 1; 
    [self layoutSubviews];  // Not sure if it is called automatically, so I called it 


} 
-(void) noticeHideKeyboard:(NSNotification *)inNotification { 
    keyboardHidden = 2; 
    [self layoutSubviews];  // Not sure if it is called automatically, so I called it 

} 

- (void) layoutSubviews 
{ 
    [super layoutSubviews]; 

    if(keyboardHidden == 1) { 
     scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height + keyboardSize.height); 
    } 
    else if(keyboardHidden == 2) { 
     scrollview.frame = CGRectMake(scrollview.frame.origin.x, scrollview.frame.origin.y, scrollview.frame.size.width, scrollview.frame.size.height - keyboardSize.height); 
    } 
} 

Anulé layoutsubviews, ahora creo que debería funcionar.

+0

El problema es que si su plumilla está utilizando el diseño automático, los tamaños rect son administrados por restricciones y configurarlos directamente no funcionará – mastergap

+1

Compruebe la respuesta editada, anulando 'layoutsubviews' debería resolver su problema ... – MuhammadBassio

4

Si entiendo el problema, recomendaría ajustar la propiedad contentInset en el UIScrollView en lugar de llamar al layoutSubviews. Tenga en cuenta que los documentos dicen:

Use esta propiedad para agregar al área de desplazamiento alrededor de su contenido. La unidad de tamaño es puntos. El valor predeterminado es UIEdgeInsetsZero.

Usted todavía tiene que escuchar para el teclado Ocultar/Mostrar notificaciones, a fin de saber cuándo ajustar la altura de su ScrollView:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; 

Luego, en keyboardWillShow usted puede hacer esto:

UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, 100, 0); 
scrollView.contentInset = insets; 

100 es la altura por la que desea ajustar su scrollView. Lo hago con un UITableView en el que tengo elementos de formulario como UITableViewCell y funciona bien.

Cuestiones relacionadas