5

我原来的纵向视图是这样的

在此处输入图像描述

当我改变方向并向左或向右旋转模拟器时,我得到

在此处输入图像描述

当我单击“个人资料”以外的任何选项卡时,选项卡栏会按照我想要的方式进行调整。

我在导航控件上使用自定义 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;
}
4

2 回答 2

0

通过在 - (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];
    }
于 2013-09-02T13:46:18.080 回答
0

您需要通过根据您的要求设置其框架来调整方向更改委托中的 uisegmentcontrol

于 2013-09-02T13:29:05.860 回答