iOS : Custom UISegmentedControl does not adjust when orientation is changed

标签 ios objective-c uisegmentedcontrol

我的原图是这样的

enter image description here

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

enter image description here

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

我在导航控件上使用自定义 UISegmentedControl。如何在屏幕旋转发生变化时立即调整标签栏的 View 。

使用 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;
}

最佳答案

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

关于iOS : Custom UISegmentedControl does not adjust when orientation is changed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18574179/

相关文章:

ios - 如何在下面的 ios 中设置像六边形的 ImageView

ios - swift 2 中翻转动画的工作原理

ios - Swift:从另一个 View Controller 调用函数使对象为零

ios - 如何检测 collectionview 超出屏幕并将其返回到其初始滚动位置?

ios - 如何在没有库的情况下从上到下制作自定义 iOS 分段控件?

ios - 如何旋转 UISegmentedControl 并增加大小以适应任意数量的文本

UITableView 中的 iOS 13 UISegmentedControl 缓存先前的选择

ios - 从文档目录加载文件时 NSData 为空

ios - 如何在项目中包含 GData?

ios - navigationItem 上的 UIImageView 尺寸错误