iphone - 当UISegmentedcontrol项更改时,为什么它会隐藏IOS 5中的自定义徽章?

标签 iphone uisegmentedcontrol

我在 UISegmentControl 上添加了自定义徽章。在 Xcode 4.0.2 SDK 4.3 中它工作正常。但在 Xcode 4.2 SDK 5.0 中,选择另一个项目时不会显示自定义徽章。我不知道为什么会出现这种情况?我正在为 IOS 4.3 Xcode 4.0.2 使用以下代码:

- (void)viewDidLoad {
    [super viewDidLoad];

    super.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"wallpaper.png"]];
    NSLog(@"Root View Loaded");

    segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                            [NSArray arrayWithObjects:
                                             @"Home",@"Surveys",@"Results",@"Create",@"Settings",
                                             nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0, 0, 310, 40);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.tintColor = [UIColor darkGrayColor];
    segmentedControl.momentary = NO;
    segmentedControl.highlighted = YES;

    segmentedControl.selectedSegmentIndex = 0;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];

    NSArray *segmentBarArray = [NSArray arrayWithObjects:
                                segmentBarItem,nil];

    [[self appDelegate] setSegmentedControl:segmentedControl];
    [[self appDelegate] setSegmentBarArray:segmentBarArray];

    [self setToolbarItems:[[self appDelegate] segmentBarArray] animated:NO];

    [[self navigationController] setToolbarHidden:NO animated:YES];
    [self.navigationItem setHidesBackButton:YES animated:YES];
    self.navigationController.toolbar.tintColor = [UIColor blackColor];

    int surveycount = [[self appDelegate] getUnreadSurveyCount];
    surveyCountBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:@"%d",surveycount]];
    [surveyCountBadge setFrame:CGRectMake(105, -10, surveyCountBadge.frame.size.width, surveyCountBadge.frame.size.height)];

    [segmentedControl addSubview:surveyCountBadge];    
    if (surveycount == 0) {
        [surveyCountBadge setHidden:YES];
    }else{
        [surveyCountBadge setHidden:NO];
    }
}
-(void)segmentAction:(id) sender{
    UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
    NSLog(@"selected index is %d",segmentedControl.selectedSegmentIndex);

    if (segmentedControl.selectedSegmentIndex == 0) {
        [self.navigationController popToRootViewControllerAnimated:YES];
    } else if(segmentedControl.selectedSegmentIndex  ==1){
        surveyForMeViewController = [[SurveyForMeView alloc] initWithNibName:@"SurveyForMeView" bundle:nil];
        [self.navigationController pushViewController:surveyForMeViewController animated:YES];

    } else if(segmentedControl.selectedSegmentIndex  ==2){
        resultViewController = [[ResultView alloc] initWithNibName:@"ResultView" bundle:nil];
        [self.navigationController pushViewController:resultViewController animated:YES];

    } else if (segmentedControl.selectedSegmentIndex ==3) {
        newSurveyViewController = [[NewSurveyView alloc] initWithNibName:@"NewSurveyView" bundle:nil];
        [self.navigationController pushViewController:newSurveyViewController animated:YES];

    } else if (segmentedControl.selectedSegmentIndex ==4) {
        settingsViewControlle = [[SettingsView alloc] initWithNibName:@"SettingsView" bundle:nil];
        [self.navigationController pushViewController:settingsViewControlle animated:YES];
    }


}

这里我还应该提到什么吗?

最佳答案

您正在将 subview 添加到segmentedControl。由于您不知道选择另一个项目时系统如何重绘控件,因此您无法确定您的 View 是否会被删除。让控件、按钮或segmentedControl 完成其工作,如果您想添加徽章,请将其添加为其父 View 的 subview 。

[surveyCountBadge setFrame:CGRectMake(105.0f+segmentedControl.frame.origin.x,
   -10.0f+sementedControl.frame.origin.y, surveyCountBadge.frame.size.width, 
    surveyCountBadge.frame.size.height)];

[[segmentedControl superView] addSubview: surveyCountBadge];

关于iphone - 当UISegmentedcontrol项更改时,为什么它会隐藏IOS 5中的自定义徽章?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260204/

相关文章:

iphone - 如何在cocos2d v2上使用CCTextField或添加UITextField

iphone - 通过 FB Graph API 发布的照片​​没有出现在墙上(以及另外 2 个 FB 问题)

iOS 7 : Segmentedcontrol won't display image colours

ios - UINavigationBar 按钮未按正确的逻辑禁用和启用

swift - 自定义 TableView + 段 Controller 代码中的格式和错误

ios - 升级到 iOS 11 后 UiNavigationbar 按钮无响应

iOS:使用谷歌登录

ios - Xcode 6.2 : no iOS simulators available?

ios - UISegmentedControl 隐藏在 titleBar 下

ios7 - 如何防止 UISegmentedControl 在 iOS 7 上缩放分段图像