2014-02-06 71 views
2

我正在開發一款應用程序,該應用程序應該可以在iOS 6和iOS 7上運行,並且具有相同的平面設計。iOS 6 UISegmentedControl與iOS 7設計

我想自定義我的UISegmentedControl有邊界,圓角半徑和所有,但我不知道如何做到這一點。到目前爲止,我只有一個平坦的背景。

有沒有人有任何建議讓iOS 6 UISegmentedControl看起來像iOS 7?

編輯:

我想有

enter image description here

,而不是

enter image description here

回答

3

您可以使用下面的代碼:

// 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

0

你可以看看一個例子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

謝謝。我希望我可以做到這一點沒有圖像,只是設置屬性「邊界」... –