UableView no cambia su tamaño correctamente en el cambio de orientación del dispositivo
-
26-10-2019 - |
Pregunta
Esta puede sonar una pregunta de novato, sin embargo, soy nuevo en iOS Dev,
Tengo UIWebView y UableView en mi vista de iPad. En shouldAutorotateToInterfaceOrientation
Los cambio de tamaño para un buen aspecto así.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTextView.frame = f;
f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTableView.frame = f;
}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTextView.frame = f;
f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTableView.frame = f;
}
return YES;
}
Ahora la pregunta: para la primera carga, la vista de la tabla se dibuja con los tamaños correctos, pero cuando cambio al modo de retrato, vuelvo al modo de paisaje, la vista Uable se vuelve más ancha de lo especificado por el cuadro. Entonces, ¿por qué sucede esto y cómo solucionar esto?
PD. También he intentado establecer los marcos para todas las celdas en el mismo método no ayudó.
Ps. Esto sucede solo en el dispositivo, en el simulador uableView se muestra correctamente
Solución
shouldAutorotateToInterfaceOrientation:
Solo devuelve un valor booleano que indique si el controlador de vista admite la orientación especificada, no debe hacer ninguna transformación de la interfaz de usuario en este método, sino que haga todo en willAnimateRotationToInterfaceOrientation:duration:
.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation duration:(NSTimeInterval)duration
{
if( interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown){
CGRect f = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTextView.frame = f;
f = CGRectMake(0, self.view.frame.size.height / 2, self.view.frame.size.width, self.view.frame.size.height / 2);
self.mTableView.frame = f;
}
if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight) {
CGRect f = CGRectMake(0, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTextView.frame = f;
f = CGRectMake(self.view.frame.size.width / 2, 0, self.view.frame.size.width / 2, self.view.frame.size.height);
self.mTableView.frame = f;
}
}
Eche un vistazo a la documentación para UIViewController, está bien explicado.
Otros consejos
¡iOS llama a esta función solo una vez! Para hacer este trabajo bien como esperas, debes agregar esta línea al (void)viewDidLoad
función:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
Y haciendo (void)orientationChanged:(id)object
función:
- (void)orientationChanged:(id)object{
UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;
if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
// some works
}else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
// another works
}
}
¡Espero que sea útil para ti!
Puede cambiar el tamaño de UableView en los modos de retrato y paisaje. UITATYVIEW Cambie el tamaño automáticamente en una relación con respecto al modo de orientación anterior. Por lo tanto, puede arreglar el tamaño deseado para ambos modos.
Espero que esto funcione para ti.
Por favor, checa el autoResizing
propiedad de las vistas.
La forma más simple es que puede hacer 2 vistas diferentes para el modo de retrato y paisaje para que cada vez que cambie el modo puede cambiar fácilmente la vista. Como esto:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
self.view = self.portraitView;
else
self.view = self.landscapeView;
[tblScore reloadData];
return YES;
}
tu usas AutoResizing
para esto no es necesario codificar para el marco.http://developer.apple.com/library/ios/#documentation/windowsviews/conceptual/viewpg_iphoneos/creatingviews/creatingviews.html