Monday, 2 September 2013

iOS : Custom UISegmentedControl does not adjust when orientation is changed

iOS : Custom UISegmentedControl does not adjust when orientation is changed

My orginal portrait view is like this

When I change the orientation and rotate the simulator left or right, I get

When I click on any tab other than "Profile", the tab bar is adjusted the
way I want.
I am using custom UISegmentedControl on navigation control. How to adjust
the views for tab-bars immediately when the rotation of the screen is
changed.
Here's my code
- (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;
}

No comments:

Post a Comment