2016-02-19 80 views
0

我非常不高興。我花了很多時間,沒有合適的結果。UISegmentedControll外觀沒有邊界鋒利的邊緣

我要重新設計(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]; 
} 

對此:

??? 
+1

你是怎麼在那麼這5個小時後重? – Wain

回答

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

可以實現這個樣子是這樣的:

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

更換[UIColor whiteColor]與分段控制的上海華盈的背景色。

+0

與答案Dh1_Bh1相同 –

0

您可以設置段的寬度(使用setWidth:forSegmentAtIndex :),以便您可以輕鬆地將左側和右側末端線段比其他線段放大(例如,大10px),然後您可以從任一末端裁剪10px有方角。您不必將其設置爲大於屏幕寬度,而是將其放在UIView中並用它裁剪兩端。

另一方面,只需使用UIControl中的一組自定義UIButton就可以製作自己的分段控件。

+0

我認爲這與大多數答案有同樣的問題。我想設置分段控件的外觀,而不是特定的實例。但仍然很好的答案。這對於實例來說是很好的解 –

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

你試過上面的代碼?因爲在clearColor case segmentControl不可見 –

+0

嘗試使用自己的顏色代替clearColor – DJ1

+0

將其與背景顏色混合在一起.. –

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

+0

這一個是最接近我的解決方案,但它也不工作與分段控制的外觀。 –