UISegmentedControl外观无边框锐边

UISegmentedControll appearance no border sharp edges

我很难过。我花了很多时间,没有合适的结果。

我想重新设计(作为外观)这个

至此

或者用编程语言...从这里:

+(void)configureSegmentedControls {

   [[UISegmentedControl appearance] setTintColor:[COLOR_PROXY highlightColor]];

   [[UISegmentedControl appearance] setBackgroundColor:[COLOR_PROXY darkBackgroundColor]];

   [[UISegmentedControl appearance] setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];

   NSDictionary *selectedAttributes = @{NSFontAttributeName:[FONT_PROXY fontNormalOfSizeSmall],
                             NSForegroundColorAttributeName:[UIColor whiteColor]};

   [[UISegmentedControl appearance] setTitleTextAttributes:selectedAttributes
                                               forState:UIControlStateSelected];

   NSDictionary *normalAttributes = @{NSFontAttributeName:[FONT_PROXY fontNormalOfSizeSmall],
               NSForegroundColorAttributeName:[COLOR_PROXY highlightColor]};

   [UISegmentedControl appearance] setTitleTextAttributes:normalAttributes
                                               forState:UIControlStateNormal];

   NSDictionary *disabledAttributes = @{NSFontAttributeName:[FONT_PROXY fontNormalOfSizeSmall],
                                    NSForegroundColorAttributeName:[COLOR_PROXY lightGrayTextColor]};

   [[UISegmentedControl appearance] setTitleTextAttributes:disabledAttributes forState:UIControlStateDisabled];
}

对此:

???

你可以这样实现:

self.segmentedControl.layer.cornerRadius = 0;
self.segmentedControl.layer.borderColor = [UIColor whiteColor].CGColor;
self.segmentedControl.layer.borderWidth = 1.5;

[UIColor whiteColor] 替换为分段控件父视图的背景色。

您可以设置线段的宽度(使用 setWidth:forSegmentAtIndex:),这样您就可以轻松地使左右端线段比其他线段大(比如大 10px),然后您可以从中裁剪 10px两端都有方角。您不必使其大于屏幕宽度,而是将其放在 UIView 中并使用它来裁剪两端。

另一方面,您可以使用 UIControl 中的一组自定义 UIButton 制作您自己的分段控件。

segmentControl.layer.borderColor = [UIColor clearColor].CGColor;
segmentControl.layer.borderWidth = 1.5;
segmentControl.tintColor = [UIColor clearColor];

如果背景色为白色则

segmentControl.layer.borderColor = [UIColor whiteColor].CGColor;
segmentControl.layer.borderWidth = 1.5;
segmentControl.tintColor = [UIColor whiteColor];

使用图片可以实现。

    [segmented setImage:nil forSegmentAtIndex:0];
    [segmented setImage:nil forSegmentAtIndex:1];
    [segmented setTintColor:[UIColor darkGrayColor]];
    [segmented setTitle:@"1" forSegmentAtIndex:0];
    [segmented setTitle:@"2" forSegmentAtIndex:1];
    [segmented setDividerImage:[self imageFromColor2:[UIColor darkGrayColor] withFrame:CGRectMake(0, 0, 1, segmented.frame.size.height)]
           forLeftSegmentState:(UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted)
             rightSegmentState:(UIControlStateNormal | UIControlStateSelected | UIControlStateHighlighted)
                    barMetrics:UIBarMetricsDefault];
    [segmented setBackgroundImage:[self imageFromColor2:[UIColor lightGrayColor] withFrame:CGRectMake(0, 0, segmented.bounds.size.width/2.0+1, segmented.frame.size.height)]
                         forState:UIControlStateNormal
                       barMetrics:UIBarMetricsDefault];

创建彩色图像:

- (UIImage *)imageFromColor2:(UIColor *)color withFrame:(CGRect)frame{
    CGRect rect = frame;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

因此我有了这个分段控件。你可以玩弄颜色,得到你想要的。

我发现的唯一解决方案(外观)是仅使用图像...

UIImage *activeImage = [[UIImage imageNamed:@"btn_bg_active"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIImage *inactiveImage = [[UIImage imageNamed:@"btn_bg_inactive"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];

[[UISegmentedControl appearance]  setBackgroundImage:inactiveImage forState:UIControlStateDisabled barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]  setBackgroundImage:inactiveImage forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]  setBackgroundImage:activeImage forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]  setBackgroundImage:inactiveImage forState:UIControlStateHighlighted barMetrics:UIBarMetricsDefault];

[[UISegmentedControl appearance]  setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]  setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateSelected rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance]  setDividerImage:[UIImage new] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];