【问题标题】:Change border color of UISegmentedControl更改 UISegmentedControl 的边框颜色
【发布时间】:2016-04-17 22:46:57
【问题描述】:

这两个部分我需要不同的边框颜色。默认情况下,我使用白色作为边框颜色。

谁能帮我解决这个问题?

【问题讨论】:

  • //添加白色边框 self.segmentedControl.layer.borderColor = [UIColor colorWithWhite:1.0f alpha:1.0f].CGColor; self.segmentedControl.layer.borderWidth = 1.0f; //使边框厚1px self.segmentedControl.layer.cornerRadius = 8.0; self.segmentedControl.layer.masksToBounds = YES;

标签: ios objective-c uisegmentedcontrol


【解决方案1】:

扩展方法段控制代码。

这是最新的 Swift 3.0 [2017 年 3 月]

的工作代码

Extension 方法被创建,作为本机段控件的扩展。

extension UISegmentedControl {
    func setSegmentStyle() {

        let segmentGrayColor = UIColor(red: 0.889415, green: 0.889436, blue:0.889424, alpha: 1.0 )

        setBackgroundImage(imageWithColor(color: backgroundColor!), for: .normal, barMetrics: .default)
        setBackgroundImage(imageWithColor(color: tintColor!), for: .selected, barMetrics: .default)
        setDividerImage(imageWithColor(color: segmentGrayColor), forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        let segAttributes: NSDictionary = [
        NSForegroundColorAttributeName: UIColor.gray,
        NSFontAttributeName: UIFont(name: "Merriweather-Regular", size: 14)!
    ]
        setTitleTextAttributes(segAttributes as [NSObject : AnyObject], for: UIControlState.normal)
        let segAttributesExtra: NSDictionary = [
        NSForegroundColorAttributeName: UIColor.white,
        NSFontAttributeName: UIFont(name: "Merriweather-Regular", size: 14)!
    ]
        setTitleTextAttributes(segAttributesExtra as [NSObject : AnyObject], for: UIControlState.selected)
        selectedSegmentIndex = -1
        self.layer.borderWidth = 1.0
        self.layer.cornerRadius = 5.0
        self.layer.borderColor = segmentGrayColor.cgColor
        self.layer.masksToBounds = true
    }

// create a 1x1 image with this color
    private func imageWithColor(color: UIColor) -> UIImage {
        let rect = CGRect(x: 0.0, y: 0.0, width:  1.0, height: 1.0)
        UIGraphicsBeginImageContext(rect.size)
        let context = UIGraphicsGetCurrentContext()
        context!.setFillColor(color.cgColor);
        context!.fill(rect);
        let image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image!
    }
}

此扩展可用于任何使用 UIsegment 控件的代码中。

myUISegment.setSegmentStyle()

这将应用应用于段控件的所有样式。

注意:在这个示例中,我删除了默认边框,更改了字体,在正常、选定模式下设置了不同的颜色,边框显式地制作了一些颜色。

根据您的要求和颜色,您可以相应地更改为绿色或任何自定义颜色,也显示在示例“segmentGrayColor”中

【讨论】:

    【解决方案2】:

    赶紧试试这个:

    class MySegmentedControl: UISegmentedControl{
    
        override func awakeFromNib() {
           super.awakeFromNib()
           for selectView in subviews{
                selectView.layer.borderColor = borderColor?.CGColor
                selectView.layer.borderWidth = CGFloat(borderWidth)
                selectView.layer.cornerRadius = CGFloat(cornerRadius)
                selectView.layer.masksToBounds = true
           }
        }
    
    }
    

    因为,每个 Segment 都是一个 View,你可以随意定制它

    【讨论】:

      【解决方案3】:

      像这样试试。

      NSArray *arri = [segment subviews];
      
      // Change the tintColor of each subview within the array:
      
      [[arri objectAtIndex:0] setTintColor:[UIColor redColor]];
      
      [[arri objectAtIndex:1] setTintColor:[UIColor greenColor]];
      

      【讨论】:

        【解决方案4】:

        一个简单的解决方案:

        //Set default tint color. This will set text color and border color
        [[UISegmentedControl appearance] setTintColor:[UIColor whiteColot]];
        // Set background image for normal and selected state. This will appear on top of the border and cover the actual border.
        [[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"btn-blue-shade-1"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
        [[UISegmentedControl appearance] setBackgroundImage:[UIImage imageNamed:@"btn-blue-shade-2"] forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
        

        【讨论】:

          【解决方案5】:

          试试这个

          segment.layer.borderColor = [UIColor colorWithRed:<#(CGFloat)#> green:<#(CGFloat)#> blue:<#(CGFloat)#> alpha:<#(CGFloat)#>];
          segment.layer.borderWidth = 1.0f;
          segment.layer.cornerRadius = 5.0f;
          

          【讨论】:

          • 我需要两个段使用不同的颜色
          • @AkhilJiji:我已经添加了一组新的代码示例,如果这有助于标记为答案,这可能会对某些人有所帮助。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-10-11
          • 2012-10-17
          • 2017-03-05
          • 1970-01-01
          • 2013-09-05
          • 1970-01-01
          • 2017-05-24
          相关资源
          最近更新 更多