Moja oryginalna widok portret jest jak teniOS custom UISegmentedControl nie dostosowuje gdy orientacja jest zmieniana
Kiedy zmienić orientację i obrócić symulator w lewo lub w prawo, mam
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;
}