2011-10-05 6 views
0

Mon application a splitview et il est activé uniquement en mode paysage. Dans masterview j'ai tableview et dans la dernière section j'ai un bouton personnalisé. Le problème est que la largeur du bouton reste dans le potrait au lieu d'utiliser l'orientation du périphérique. J'ai essayé de jouer avec setAutoresizingMask sur le bouton et la vue, mais ça ne marche pas non plus.iPad, redimensionner bouton personnalisé dans viewForHeaderInSection

- (UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) sectionNum { 
    if (sectionNum == 4) { 
     CGRect frameSize; 

     if (UIInterfaceOrientationIsLandscape([[UIDevice currentDevice] orientation])) { 
      frameSize = CGRectMake(45.0, 0.0, 600.0, 44.0); 
     } else { 
      frameSize = CGRectMake(45.0, 0.0, 680.0, 44.0); 
     } 

     UIView *btnView = [[[UIView alloc] initWithFrame:frameSize] autorelease]; 

     // button 
     UIButton* btnFind = [UIButton buttonWithType:UIButtonTypeCustom]; 
     [btnFind setBackgroundImage:[[UIImage imageNamed:@"buttonblue.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateNormal]; 
     [btnFind setBackgroundImage:[[UIImage imageNamed:@"buttonred.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0] forState:UIControlStateHighlighted]; 

     btnFind.frame = frameSize; 
     btnFind.tag = 1; 

     [btnFind setTitle:@"Find" forState:UIControlStateNormal]; 
     [btnFind addTarget:self action:@selector(doSearchDoc:) forControlEvents:UIControlEventTouchUpInside]; 
     //[btnFind setAutoresizingMask:UIViewAutoresizingFlexibleRightMargin]; 

     //[btnView setAutoresizingMask:UIViewAutoresizingFlexibleWidth]; 
     [btnView addSubview:btnFind]; 

     return btnView; 
    } 

    return nil; 
} 

Quelqu'un peut-il s'il vous plaît laissez-moi savoir comment résoudre ce problème.

Merci.

Répondre

1

J'ai eu d'autres problèmes que l'en-tête n'a pas été actualisé lors de la rotation de l'appareil, à savoir. la méthode ci-dessus n'a pas été invoquée dans ma situation, vous pouvez vérifier si c'est le cas aussi pour vous. La solution que j'adoptai était d'invoquer reloadData dans la méthode de rotation:

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { 
    // This solves an issue that the header doesn't show when roating device (ok on simulator) 
    NSIndexPath* ipselected = [self.tableView indexPathForSelectedRow]; 
    [self.tableView reloadData]; 
    [self.tableView selectRowAtIndexPath:ipselected animated:false scrollPosition:UITableViewScrollPositionMiddle]; 
} 

Bien sûr, il est utilisable si vous pouvez vous permettre un rechargement complet lors de la rotation du dispositif, mais SITI généralement un moment où l'utilisateur mieux accepter 0,5 s retarder car il/elle est occupé à déplacer l'appareil.

Alternativement, vous pouvez garder un ivar sur votre bouton et changer son cadre dans la méthode ci-dessus.

+0

merci beaucoup, cela fonctionne bien. – Inoel

+0

dans mon cas cela fonctionne mieux: j'ai modifié votre code et remplacer .reloadData par .reloadSections: [NSIndexSet indexSetWithIndex: btnSectionIndex] withRowAnimation: NO]; – Inoel

Questions connexes