iOS:自定义UISegmentedControl在更改方向时不会调整

时间:2013-09-02 13:13:50

标签: ios objective-c uisegmentedcontrol

我的原始人像观点是这样的

enter image description here

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

enter image description here

当我点击“个人资料”以外的任何标签时,标签栏会按我想要的方式调整。

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

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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];
    }