UITableView ne change pas dimensionner correctement sur le changement d'orientation de l'appareil

StackOverflow https://stackoverflow.com/questions/8338963

Question

Cela peut sembler une question de débutant, mais je suis nouveau dev iOS,

J'ai UIWebView et UITableView sur mon point de vue iPad. Dans shouldAutorotateToInterfaceOrientation je les remets à la côte pour bien ressembler à ceci.

    - (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;
    }

Maintenant la question: Pour la première charge l'affichage du tableau est dessiné avec des tailles correctes mais quand je passe en mode portrait puis de nouveau en mode paysage le UITableView devient plus large que celle spécifiée par image. Alors, pourquoi cela se produit et comment résoudre ce problème?

PS. Je l'ai aussi essayé de définir les cadres pour toutes les cellules de la même méthode n'a pas aidé.

PSS. Cela se produit uniquement sur l'appareil, le simulateur UITableView est correctement affiché

Était-ce utile?

La solution

shouldAutorotateToInterfaceOrientation: renvoie uniquement une valeur booléenne indiquant si le contrôleur de vue prend en charge l'orientation spécifiée, vous ne devriez pas faire toute transformation de l'interface utilisateur dans cette méthode, au lieu tout 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;        
    }
}

Jetez un oeil à la documentation UIViewController, il est bien expliqué.

Autres conseils

iOS appelle cette fonction juste une fois! Pour faire ce travail agréable que vous attendez, vous devez ajouter cette ligne dans la fonction (void)viewDidLoad:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];

Et faisant fonction (void)orientationChanged:(id)object:

- (void)orientationChanged:(id)object{
    UIInterfaceOrientation interfaceOrientation = [UIApplication sharedApplication] statusBarOrientation;

    if(UIInterfaceOrientationIsPortrait(interfaceOrientation)){
        // some works
    }else if(UIInterfaceOrientationIsLandscape(interfaceOrientation)){
        // another works
    }
}

Je l'espère vous être utile!

Vous pouvez modifier la taille de UITableView dans les deux modes portrait et paysage. UITableView taille de changement automatique dans un rapport par rapport au mode d'orientation précédente. Ainsi, vous pouvez fixer la taille désirée pour les deux modes.

Hope ce travail pour vous.

S'il vous plaît vérifier la propriété autoResizing des vues.

La façon la plus simple est que vous pouvez faire 2 point de vue différent pour le mode portrait et paysage donc chaque fois que le mode change, vous pouvez facilement changer la vue. Comme ceci:

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    if (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown)
        self.view = self.portraitView;
    else 
        self.view = self.landscapeView;
    [tblScore reloadData];
    return YES;
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top