2013-09-02 19 views
5

Moja oryginalna widok portret jest jak teniOS custom UISegmentedControl nie dostosowuje gdy orientacja jest zmieniana

enter image description here

Kiedy zmienić orientację i obrócić symulator w lewo lub w prawo, mam

enter image description here

Po kliknięciu dowolnej zakładki innej niż "Profil" pasek zakładek jest dostosowywany zgodnie z potrzebami.

Używam niestandardowego UISegmentedControl na kontroli nawigacji. Jak zmienić widoki tab-barów natychmiast po zmianie obrotu ekranu.

Korzystanie z Xcode 4.6 i wdrożenie dotyczy wszystkich wersji systemu iOS.

Oto mój kod

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    //Tapped outside to hide keyboard 
    UITapGestureRecognizer *tapped = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; 
    [self.view addGestureRecognizer:tapped]; 

    tapped.cancelsTouchesInView = NO; 

    [self.navigationItem setHidesBackButton:YES animated:YES]; 

    profileSegmentControl = [[UISegmentedControl alloc]initWithItems:[NSArray arrayWithObjects: @"Profile", @"List", @"Scan", @"Collaborate", @"Logout", nil]]; 


    [profileSegmentControl addTarget:self action:@selector(profileButton) forControlEvents:UIControlEventValueChanged]; 
    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    { 
     if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) 
     { 
      UIFont *font = [UIFont boldSystemFontOfSize:10.5f]; 
      NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont]; 
      [profileSegmentControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
      profileSegmentControl.frame = CGRectMake(0, 0, 318, 30); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 470, 30); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 470, 30); 
     } 
     [self.view setNeedsLayout]; 
    } 
    else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) 
     { 

      profileSegmentControl.frame = CGRectMake(0, 0, 758, 40); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 994, 40); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 994, 40); 
     } 
     [self.view setNeedsLayout]; 
    } 

    profileSegmentControl.segmentedControlStyle = UISegmentedControlStyleBar; 
    profileSegmentControl.momentary = YES; 

    //[profileSegmentControl sizeToFit]; 

    UINavigationBar *bar = [self.navigationController navigationBar]; 
    [bar setTintColor:[UIColor blackColor]]; 



    UIBarButtonItem *profileSegmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:profileSegmentControl]; 
    self.navigationItem.rightBarButtonItem = profileSegmentBarItem; 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation 
{ 
    return [self.presentingViewController shouldAutorotateToInterfaceOrientation:toInterfaceOrientation]; 
} 



-(BOOL)shouldAutorotate 
{ 
    return YES; 
} 

- (NSUInteger)supportedInterfaceOrientations 
{ 
    return UIInterfaceOrientationMaskAllButUpsideDown; 
} 

Odpowiedz

0

trzeba dostosować uisegmentcontrol w zmianie orientacji delegata poprzez ustawienie jej ramkę zgodnie z własnymi WYMAGANIA

0

realizowany przez cięcie wklejając poniższy kod - (NSUInteger) supportedInterfaceOrientations metody

if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) 
    { 
     if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) 
     { 
      UIFont *font = [UIFont boldSystemFontOfSize:10.5f]; 
      NSDictionary *attributes = [NSDictionary dictionaryWithObject:font forKey:UITextAttributeFont]; 
      [profileSegmentControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
      profileSegmentControl.frame = CGRectMake(0, 0, 318, 30); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 470, 30); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 470, 30); 
     } 
     [self.view setNeedsLayout]; 
    } 
    else if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    { 
     if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationPortrait) 
     { 

      profileSegmentControl.frame = CGRectMake(0, 0, 758, 40); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeLeft) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 994, 40); 
     } 
     else if([UIApplication sharedApplication].statusBarOrientation == UIInterfaceOrientationLandscapeRight) 
     { 
      profileSegmentControl.frame = CGRectMake(0, 0, 994, 40); 
     } 
     [self.view setNeedsLayout]; 
    }