【问题标题】:Remove UISegmentedControl separators completely. (iphone)完全删除 UISegmentedControl 分隔符。 (苹果手机)
【发布时间】:2012-03-08 01:35:36
【问题描述】:

有没有办法完全删除分隔 UISegmentedControl 中两个段的线?

设置segmentedControlStyle 没有帮助。

【问题讨论】:

    标签: ios uisegmentedcontrol


    【解决方案1】:

    不,没有。您应该考虑手动创建 UISegmentedControl 功能。也许使用两个 UIButtons。

    【讨论】:

    • 从 iOS 5.0 开始有一种方法,使用像下面建议的 Naveed 这样的薄 UIImage。
    【解决方案2】:

    使用此方法更改您的分段分隔线图像:

    [segmentControl setDividerImage:dividerimg
                forLeftSegmentState:UIControlStateNormal
                  rightSegmentState:UIControlStateNormal
                         barMetrics:UIBarMetricsDefault];
    

    【讨论】:

      【解决方案3】:

      我在 iOS7 中使用了segmentcontrol.tintColor = [UIColor clearColor];,它似乎可以工作。

      【讨论】:

      • 不,这会让整个 UISegmentedControl 不可见!!
      • 使色调颜色清晰也使标题颜色清晰。我认为这不是一个好的解决方案。
      • 如果您还调用 setTitleTextAttributes 并为每个正常、突出显示和选定状态传递带有颜色的字体属性,这是一个很好的解决方案。
      • 是的,这正是我所需要的! ty @par 和 palaniraja
      【解决方案4】:

      以下代码将完全符合您的要求。

      UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, self.segControlOut.frame.size.height), NO, 0.0);
      UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      [self.segControlOut setDividerImage:blank
                      forLeftSegmentState:UIControlStateNormal
                        rightSegmentState:UIControlStateNormal
                               barMetrics:UIBarMetricsDefault];
      

      【讨论】:

      • 对于空白图片首选[UIImage new]
      【解决方案5】:

      您可以通过设置黑色图像从 UISegmentedControl 中移除所有背景、边框和分隔符。或者您可以设置自定义颜色。

      以下示例删除所有边框/分隔符,并将背景设置为 0.2 alpha 的白色,并将选定的段设置为 alpha 1.0 的白色

      [self.segmentedControl setBackgroundImage:[self imageWithColor:[UIColor colorWithWhite:1.0 alpha:0.2]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
      [self.segmentedControl setBackgroundImage:[self imageWithColor:[UIColor colorWithWhite:1.0 alpha:1.0]] forState:UIControlStateSelected  barMetrics:UIBarMetricsDefault];
      
      - (UIImage *)imageWithColor:(UIColor *)color {
          CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
          UIGraphicsBeginImageContext(rect.size);
          CGContextRef context = UIGraphicsGetCurrentContext();
      
          CGContextSetFillColorWithColor(context, [color CGColor]);
          CGContextFillRect(context, rect);
      
          UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
          UIGraphicsEndImageContext();
      
          return image;
      }
      

      【讨论】:

      • 我目前正在这样做,但我遇到了问题。如果我曾经为正常状态调用 setBackgroundImage,并且我尝试以编程方式更改 UISegmentedControl 的选定索引,则图标的色调不会更新。只有突出显示的颜色会更新。因此,您有一个几乎看不到的暗淡图标。有其他人看到或解决过这个问题吗?
      【解决方案6】:

      我将之前的答案组合成一个 Swift 扩展。

      我想删除所有边框和分隔线。此代码仅使用正常的段颜色,例如选择/取消选择的色调和背景。

      打电话

      segmentedControl.removeBorders()
      

      这是扩展名

      extension UISegmentedControl {
          func removeBorders() {
              setBackgroundImage(imageWithColor(backgroundColor!), forState: .Normal, barMetrics: .Default)
              setBackgroundImage(imageWithColor(tintColor!), forState: .Selected, barMetrics: .Default)
              setDividerImage(imageWithColor(UIColor.clearColor()), forLeftSegmentState: .Normal, rightSegmentState: .Normal, barMetrics: .Default)
          }
      
          // create a 1x1 image with this color
          private func imageWithColor(color: UIColor) -> UIImage {
              let rect = CGRectMake(0.0, 0.0, 1.0, 1.0)
              UIGraphicsBeginImageContext(rect.size)
              let context = UIGraphicsGetCurrentContext()
              CGContextSetFillColorWithColor(context, color.CGColor);
              CGContextFillRect(context, rect);
              let image = UIGraphicsGetImageFromCurrentImageContext();
              UIGraphicsEndImageContext();
              return image
          }
      }
      

      【讨论】:

      • 我必须使用显式颜色来代替backgroundColor!tintColor!,否则在展开时您可能会发现 nil。否则,太棒了!
      • @dperk 如果代码是通用的,它应该处理背景和色调设置为零。这些可以是 removeBorders() 的参数,也可以是未设置时的默认值。不确定控件在未设置时如何找到这些默认值,但可以找出并执行相同操作。现在确保设置了 tintColor 和 backgroundColor ;)
      • 问题:每次我点击一个片段,我都会在该片段的背景中看到一个浅灰色的矩形。它只在我点击时出现并在点击后消失。它看起来很丑陋。我将如何删除它?
      • 通过调用 setBackgroundImage 与: imageWithColor(UIColor.clearColor() ) 为 .Highlighted 状态。
      • 我目前正在这样做,但我遇到了问题。如果我曾经为正常状态调用 setBackgroundImage,并且我尝试以编程方式更改 UISegmentedControl 的选定索引,则图标的色调不会更新。只有突出显示的颜色会更新。因此,您有一个几乎看不到的暗淡图标。有其他人看到或解决过这个问题吗?
      【解决方案7】:

      @naveed 和 @Jon Setting 的回答拯救了我的一天!

      但作为一个完美主义者,我很沮丧地发现这段代码还在控件的外边界留下了漏洞:

      所以我添加了几行代码来摆脱这些代码并使它看起来更整洁:

      完整的代码是

      CGFloat h = self.segControlOut.frame.size.height;
      UIGraphicsBeginImageContextWithOptions(CGSizeMake(1, h), NO, 0.0);
      
      // draw the missing dots with the control border color
      [self.segControlOut.tintColor set];
      UIRectFrame( CGRectMake(0, -1, 1, 2.5) );
      UIRectFrame( CGRectMake(0, h-1.5, 1, 2.5) );
      
      UIImage *blank = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext();
      [self.segControlOut setDividerImage:blank
                      forLeftSegmentState:UIControlStateNormal
                        rightSegmentState:UIControlStateNormal
                               barMetrics:UIBarMetricsDefault];
      

      【讨论】:

        【解决方案8】:

        如果有人想要这种UISegmentedControl View-

        已编辑:- Swift 3.0 扩展

            extension UISegmentedControl {
        
            func customizeAppearance(for height: Int) {
        
                setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.white], for:.normal)
                setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.white], for:.selected)
                setDividerImage(UIImage().colored(with: .clear, size: CGSize(width: 1, height: height)), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
                setBackgroundImage(UIImage().colored(with: .clear, size: CGSize(width: 1, height: height)), for: .normal, barMetrics: .default)
                setBackgroundImage(UIImage().colored(with: UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha: 1.0), size: CGSize(width: 1, height: height)), for: .selected, barMetrics: .default);
        
                for  borderview in subviews {
                    let upperBorder: CALayer = CALayer()
                    upperBorder.backgroundColor = UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha: 1.0).cgColor
                    upperBorder.frame = CGRect(x: 0, y: borderview.frame.size.height-1, width: borderview.frame.size.width, height: 1)
                    borderview.layer.addSublayer(upperBorder)
                }
        
            }
        }
        
        extension UIImage {
        
            func colored(with color: UIColor, size: CGSize) -> UIImage {
                UIGraphicsBeginImageContext(size)
                let context = UIGraphicsGetCurrentContext()
                context!.setFillColor(color.cgColor);
                let rect = CGRect(origin: CGPoint(x: 0, y: 0), size: size)
                context!.fill(rect);
                let image = UIGraphicsGetImageFromCurrentImageContext();
                UIGraphicsEndImageContext();
                return image!
            }
        }
        

        UIImage 扩展生成具有所需大小的彩色图像。

        customizeAppearance 中强制使用height 参数可避免在构建context 时出现任何崩溃。

        您可以通过提取每个自定义属性并将其作为函数的参数来使其更加可重用;)

        这是示例代码,在 iOS 9 上进行了测试,对我来说运行良好。

        viewDidLoad中添加这几行代码-

        [yourSegmentControl setTitleTextAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"Roboto-black" size:13.0],NSForegroundColorAttributeName:[UIColor whiteColor] }forState:UIControlStateSelected];
        
        [yourSegmentControl setTitleTextAttributes:@{ NSFontAttributeName:[UIFont fontWithName:@"Roboto-black" size:13.0],NSForegroundColorAttributeName:[UIColor whiteColor] }forState:UIControlStateNormal];
        
        [yourSegmentControl setDividerImage:[self imageWithColor:[UIColor clearColor]] forLeftSegmentState:UIControlStateNormal rightSegmentState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        
        [yourSegmentControl setBackgroundImage:[self imageWithColor:[UIColor clearColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        
        [yourSegmentControl setBackgroundImage:[self imageWithColor:[UIColor colorWithRed:215/255.0 green:0 blue:30/255.0 alpha:1.0]] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        
        for (UIView *borderview in yourSegmentControl.subviews) {
        
            CALayer *upperBorder = [CALayer layer];
        
            upperBorder.backgroundColor = [UIColor colorWithRed:215/255.0 green:0 blue:30/255.0 alpha:1.0].CGColor;
        
            upperBorder.frame = CGRectMake(0, borderview.frame.size.height-1, borderview.frame.size.width, 1.0f);
        
            [borderview.layer addSublayer:upperBorder];
        }
        

        - (UIImage *)imageWithColor:(UIColor *)color {
            CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
            UIGraphicsBeginImageContext(rect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
        
            CGContextSetFillColorWithColor(context, [color CGColor]);
            CGContextFillRect(context, rect);
        
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
            return image;
        }
        

        这是在 Swift 中实现相同视图的 Swift 扩展-

        yourSegmentControl.setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Normal)
        
        yourSegmentControl.setTitleTextAttributes([NSFontAttributeName:UIFont(name:"Helvetica Neue", size:13.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()], forState:UIControlState.Selected)
        
        yourSegmentControl.setDividerImage(self.imageWithColor(UIColor.clearColor()), forLeftSegmentState: UIControlState.Normal, rightSegmentState: UIControlState.Normal, barMetrics: UIBarMetrics.Default)
        
        yourSegmentControl.setBackgroundImage(self.imageWithColor(UIColor.clearColor()), forState:UIControlState.Normal, barMetrics:UIBarMetrics.Default)
        
        yourSegmentControl.setBackgroundImage(self.imageWithColor(UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha:1.0)), forState:UIControlState.Selected, barMetrics:UIBarMetrics.Default);
        
        for  borderview in yourSegmentControl.subviews {
        
        let upperBorder: CALayer = CALayer()
        upperBorder.backgroundColor = UIColor.init(red: 215/255.0, green: 0.0, blue: 30/255.0, alpha: 1.0).CGColor
        upperBorder.frame = CGRectMake(0, borderview.frame.size.height-1, borderview.frame.size.width, 1.0);
        borderview.layer .addSublayer(upperBorder);
        
        }
        

        func imageWithColor(color: UIColor) -> UIImage {
        
            let rect = CGRectMake(0.0, 0.0, 1.0, yourSegmentControl.frame.size.height)
            UIGraphicsBeginImageContext(rect.size)
            let context = UIGraphicsGetCurrentContext()
            CGContextSetFillColorWithColor(context, color.CGColor);
            CGContextFillRect(context, rect);
            let image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
            return image
        
        }
        

        【讨论】:

        • 好像去掉了控件的圆角半径。知道如何将其添加回来吗?
        • 太棒了!非常感谢!
        【解决方案9】:
        - (void) configSegmentControl {
            [segmentControl setBackgroundImage:[self imageWithColor:[UIColor blueColor]] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
            [segmentControl setBackgroundImage:[self imageWithColor:[UIColor redColor]] forState:UIControlStateSelected  barMetrics:UIBarMetricsDefault];
            segmentControl.layer.cornerRadius = 4.0f;
            segmentControl.clipsToBounds = YES;
        }
        
        - (UIImage *)imageWithColor:(UIColor *)color {
            CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
            UIGraphicsBeginImageContext(rect.size);
            CGContextRef context = UIGraphicsGetCurrentContext();
        
            CGContextSetFillColorWithColor(context, [color CGColor]);
            CGContextFillRect(context, rect);
        
            UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        
            return image;
        }
        

        【讨论】:

          猜你喜欢
          • 2015-07-26
          • 2010-10-07
          • 1970-01-01
          • 1970-01-01
          • 2011-08-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多