iOS 6 UISegmentedControl与iOS 7设计

问题描述:

我正在开发一款应用程序,该应用程序应该可以在iOS 6和iOS 7上运行,并且具有相同的平面设计。iOS 6 UISegmentedControl与iOS 7设计

我想自定义我的UISegmentedControl有边界,圆角半径和所有,但我不知道如何做到这一点。到目前为止,我只有一个平坦的背景。

有没有人有任何建议让iOS 6 UISegmentedControl看起来像iOS 7?

编辑:

我想有

enter image description here

,而不是

enter image description here

您可以使用下面的代码:

// To set colour of text 
     NSDictionary *attributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; 
     [segmentedControl setTitleTextAttributes:attributes forState:UIControlStateNormal]; 
     NSDictionary *highlightedAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:UITextAttributeTextColor]; 
     [segmentedControl setTitleTextAttributes:highlightedAttributes forState:UIControlStateHighlighted]; 

     // Change color of selected segment 

     segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar; 

     UIColor *newTintColor = [UIColor colorWithRed: 255/255.0 green:100/255.0 blue:100/255.0 alpha:1.0]; 
     segmentedControl.tintColor = newTintColor; 

     UIColor *newSelectedTintColor = [UIColor clearColor]; 
     [[[segmentedControl subviews] objectAtIndex:0] setTintColor:newSelectedTintColor]; 

而对于塞汀圆角您可以使用下面的代码:

// Add rounded yellow corner to segmented controll view 
[segmentedControl.layer setCornerRadius:4.0f]; 
[segmentedControl.layer setBorderColor:[UIColor colorWithRed:1.0 green:0.7 blue:0.14 alpha:1.0].CGColor]; 
[segmentedControl.layer setBorderWidth:1.5f]; 
[segmentedControl.layer setShadowColor:[UIColor blackColor].CGColor]; 
[segmentedControl.layer setShadowOpacity:0.8]; 
[segmentedControl.layer setShadowRadius:3.0]; 
[segmentedControl.layer setShadowOffset:CGSizeMake(2.0, 2.0)]; 
+0

事情是,这段代码不会创建任何边界或半径:/ –

+0

现在你可以检查编辑:) – Ashutosh

+0

这doesn即使在调整之后,iOS 6也不适合我。 – CVertex

你可以看看一个例子here或采取custom control和变化其图像适合您的需求。

这里的UISegmentedControl你如何继承的例子:

@implementation CustomSegmentedControl 
-(id)initWithItems:(NSArray *)items 
{ 
    self = [super initWithItems:items]; 
    if (self) { 

     [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-none-selected"] 
      forLeftSegmentState:UIControlStateNormal 
      rightSegmentState:UIControlStateNormal 
        barMetrics:UIBarMetricsDefault]; 
     [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-left-selected"] 
      forLeftSegmentState:UIControlStateSelected 
      rightSegmentState:UIControlStateNormal 
        barMetrics:UIBarMetricsDefault]; 
     [self setDividerImage:[UIImage imageNamed:@"segmented-control-divider-right-selected"] 
      forLeftSegmentState:UIControlStateNormal 
      rightSegmentState:UIControlStateSelected 
        barMetrics:UIBarMetricsDefault]; 

    // Set background images 
    UIImage *normalBackgroundImage = [UIImage imageNamed:@"segmented-control-normal"]; 
    [self setBackgroundImage:normalBackgroundImage 
        forState:UIControlStateNormal 
        barMetrics:UIBarMetricsDefault]; 
    UIImage *selectedBackgroundImage = [UIImage imageNamed:@"segmented-control-selected"]; 
    [self setBackgroundImage:selectedBackgroundImage 
        forState:UIControlStateSelected 
        barMetrics:UIBarMetricsDefault]; 

    [self setBackgroundImage:selectedBackgroundImage 
        forState:UIControlStateHighlighted 
        barMetrics:UIBarMetricsDefault]; 

    double dividerImageWidth = [self dividerImageForLeftSegmentState:UIControlStateHighlighted rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault].size.width; 
     [self setContentPositionAdjustment:UIOffsetMake(dividerImageWidth/2, 0) 
          forSegmentType:UISegmentedControlSegmentLeft 
           barMetrics:UIBarMetricsDefault]; 
     [self setContentPositionAdjustment:UIOffsetMake(- dividerImageWidth/2, 0) 
          forSegmentType:UISegmentedControlSegmentRight 
           barMetrics:UIBarMetricsDefault]; 
return self; 
} 
+0

谢谢。我希望我可以做到这一点没有图像,只是设置属性“边界”... –

我终于使用了这个子模块。它的工作:

https://github.com/pepibumur/PPiFlatSegmentedControl