1

所以这里是代码:

UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 180, 35)];
    //[segmentedCtrl setTintColor:[UIColor brownColor]];
    [segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    [segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
    [segmentedCtrl setBackgroundColor:[UIColor clearColor]];

    [segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:nil] atIndex:0 animated:NO];
    [segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:nil] atIndex:1 animated:NO];
    [segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:nil] atIndex:2 animated:NO];

    [segmentedCtrl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    [segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBar];
    //[segmentedCtrl setMomentary:YES];
    [segmentedCtrl.layer setBorderWidth:1.0];
    [segmentedCtrl.layer setCornerRadius:5.0];

但是,当我按下分段控件时,我得到了这个:

在此处输入图像描述

任何想法删除蓝色突出显示?

这是我压缩的一个示例项目,它显示了这个问题:

http://dl.dropbox.com/u/19680269/Test.zip

4

4 回答 4

1

我试过你的代码...

我通过添加此代码解决了这个问题

在此处输入图像描述

[segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBezeled];

所以整个代码会是这样的

UISegmentedControl * segmentedCtrl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 180, 35)];
//[segmentedCtrl setTintColor:[UIColor brownColor]];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[segmentedCtrl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[segmentedCtrl setBackgroundColor:[UIColor clearColor]];
[segmentedCtrl setSegmentedControlStyle:UISegmentedControlStyleBezeled];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:nil] atIndex:0 animated:NO];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:nil] atIndex:1 animated:NO];
[segmentedCtrl insertSegmentWithImage:[UIImage imageNamed:nil] atIndex:2 animated:NO];

[segmentedCtrl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
//[segmentedCtrl setMomentary:YES];
[segmentedCtrl.layer setBorderWidth:1.0];
[segmentedCtrl.layer setCornerRadius:5.0];
于 2012-05-25T08:04:28.790 回答
0

您可以添加一个分隔符 png 文件:

[segmentedControl setDividerImage:[UIImage imageNamed:@"separator.png"] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

ps:

你为什么将它两次添加到你的视图中?

[self.view addSubview:segmentedControl];

编辑:

如果您真的不需要分段之间的分隔符,您可以这样删除它们:

for (UIView* aSubV in segmentedControl.subviews) {
    NSLog(@"•••");
    NSLog(@"aSubV2: %@",[aSubV.subviews objectAtIndex:1]);
    UIView* separatorToRemove = [aSubV.subviews objectAtIndex:1];
    [separatorToRemove removeFromSuperview];
}
于 2012-05-25T08:28:21.043 回答
0
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

添加此行修复viewDidLoadMCSegmentedControlDemoViewController.m该错误。蓝色是普通段视图的高亮颜色。

当您为普通段设置背景时,它不会完全覆盖。所以你会看到边缘的蓝色。

使其条形样式使您的图像覆盖整个片段。因此,您不会看到蓝色。

于 2012-05-25T08:59:36.123 回答
0

我不确定这是否正是您正在寻找的,但如果您使用下面的代码,结果将是:

在此处输入图像描述

更改您的代码:

  • 你加segmentedControlself.view两次 - 删除了第一次出现
  • 添加[segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];

段之间有一条 1px 的绿线 - 这是由于您的线:

segmentedControl.tintColor = [UIColor colorWithRed:.0 green:.6 blue:.0 alpha:1.0];

如果您希望线条具有其他颜色,只需更改tintColor属性即可。

完整的viewDidLoad样子:

代码没有在其他任何地方修改。

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor colorWithWhite:0.95 alpha:1.0];
    NSArray *items = [NSArray arrayWithObjects:
                      @"one",
                      @"two",
                      [UIImage imageNamed:@"star.png"],
                      nil];

    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:items];

    // set frame, add to view, set target and action for value change as usual
    segmentedControl.frame = CGRectMake(10.0f, 208.0f, 300.0f, 44.0f);
    //[self.view addSubview:segmentedControl];
    [segmentedControl addTarget:self action:@selector(segmentedControlDidChange:) forControlEvents:UIControlEventValueChanged];

    [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];

    [segmentedControl setBackgroundImage:[UIImage imageNamed:@"btn-gradient-brown"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
    segmentedControl.selectedSegmentIndex = 1;

    // Set a tint color
    segmentedControl.tintColor = [UIColor colorWithRed:.0 green:.6 blue:.0 alpha:1.0];

    [self.view addSubview:segmentedControl];
    [segmentedControl release];    
}
于 2012-05-25T14:30:20.950 回答