xem chân dung ban đầu của tôi là như thế nàyiOS: Tuỳ chỉnh UISegmentedControl không điều chỉnh khi định hướng được thay đổi
Khi tôi thay đổi hướng và xoay mô phỏng sang trái hoặc phải, tôi nhận được
Khi tôi nhấp vào bất kỳ tab nào ngoài "Hồ sơ", thanh tab được điều chỉnh theo cách tôi muốn.
Tôi đang sử dụng UISegmentedControl tùy chỉnh trên điều khiển điều hướng. Cách điều chỉnh chế độ xem cho thanh tab ngay lập tức khi xoay màn hình được thay đổi.
Sử dụng Xcode 4.6 và triển khai là dành cho tất cả các phiên bản iOS.
Dưới đây là mã của tôi
- (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;
}