UISegmentedControll外观无边框锋利边缘

时间:2016-02-19 13:20:04

标签: ios objective-c uisegmentedcontrol

我非常沮丧。我花了很多时间而且没有合适的结果。

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

enter image description here

到这个

enter image description here

或者用程序化的话......来自:

+(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];
}

到此:

???

5 个答案:

答案 0 :(得分:2)

你可以这样做:

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

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

答案 1 :(得分:0)

你可以设置段的宽度(使用setWidth:forSegmentAtIndex :),这样你就可以轻松地使左右端段比其他段更大(比大10px更大)然后你可以从任何一端裁掉10px并且有方角。您不必将其放大到屏幕宽度,而是将其放在UIView中并用它来裁剪末端。

另一方面,只需使用UIControl中的一组自定义UIButton进行自己的分段控制。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

使用图像可以实现。

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

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

result of code

答案 4 :(得分:-1)

只有解决方案(外观)我发现只使用图像...

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