私の元の縦長ビューはこのようなものです
向きを変えてシミュレーターを左右に回転させると、
「プロファイル」以外のタブをクリックすると、タブバーが思いどおりに調整されます。
ナビゲーション コントロールでカスタム UISegmentedControl を使用しています。画面の回転が変更されたときにすぐにタブバーのビューを調整する方法。
Xcode 4.6 を使用して展開すると、すべての iOS バージョンが対象になります。
これが私のコードです
- (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;
}