iOS:更改方向时,自定义UISegmentedControl不会调整
我的原始肖像是这样的 当我改变方向并向左或向右旋转模拟器时,我得到 当我单击除“配置文件”以外的任何选项卡时,选项卡栏将按照我想要的方式进行调整 我在导航控件上使用自定义UISegmentedControl。如何在更改屏幕旋转时立即调整选项卡栏的视图 使用Xcode 4.6和部署适用于所有iOS版本 这是我的密码iOS:更改方向时,自定义UISegmentedControl不会调整,ios,objective-c,uisegmentedcontrol,Ios,Objective C,Uisegmentedcontrol,我的原始肖像是这样的 当我改变方向并向左或向右旋转模拟器时,我得到 当我单击除“配置文件”以外的任何选项卡时,选项卡栏将按照我想要的方式进行调整 我在导航控件上使用自定义UISegmentedControl。如何在更改屏幕旋转时立即调整选项卡栏的视图 使用Xcode 4.6和部署适用于所有iOS版本 这是我的密码 - (void)viewDidLoad { [super viewDidLoad]; //Tapped outside to hide keyboard U
- (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;
}
您需要在方向更改委托中调整uisegmentcontrol,方法是根据您的要求设置其框架在-(NSUInteger)supportedInterfaceOrientations方法中剪切粘贴以下代码来实现
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];
}