【问题标题】:iOS 6 UISegmentedControl with iOS 7 designiOS 6 UISegmentedControl 与 iOS 7 设计
【发布时间】:2014-03-03 10:51:40
【问题描述】:

我正在开发一个应该在 iOS 6 和 iOS 7 上运行的应用程序,并且两者都具有相同的平面设计。

我正在尝试自定义 UISegmentedControl 以使其具有边框、圆角半径等所有内容,但我不知道该怎么做。到目前为止,我只有一个平坦的背景。

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

编辑:

我想拥有

而不是

【问题讨论】:

    标签: ios ios6 ios7 uisegmentedcontrol


    【解决方案1】:

    您可以使用以下代码:

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

    【讨论】:

    • 问题是这段代码没有创建任何边框或半径:/
    • 这对我在 iOS 6 上不起作用,即使经过调整。
    【解决方案2】:

    您可以查看示例 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;
    }
    

    【讨论】:

    • 谢谢。我希望我可以在没有图像的情况下做到这一点,只需设置一个属性“边框”......
    【解决方案3】:

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

    https://github.com/pepibumur/PPiFlatSegmentedControl

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-20
      • 1970-01-01
      相关资源
      最近更新 更多