UISegmentedControl非常奇怪的问题

时间:2012-05-25 07:09:18

标签: iphone objective-c ios ipad

所以这是代码:

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];

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

enter image description here

有什么想法删除蓝色高光?

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

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

4 个答案:

答案 0 :(得分:1)

我尝试了你的代码......

我通过添加此代码

解决了这个问题

enter image description here

[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];

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

答案 2 :(得分:0)

segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;

viewDidLoad的{​​{1}}中添加此行修复了该错误。 蓝色是正常分段视图的高亮颜色。

为正常细分设置背景时,它不会完全覆盖。所以你看到边缘的蓝色。

使其设置为条形使您的图像覆盖整个细分。因此你不会看到蓝色。

答案 3 :(得分:0)

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

enter image description here

您的代码更改:

  • 您将segmentedControl添加到self.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];    
}