【问题标题】:Add Border to UISegmentedControl为 UISegmentedControl 添加边框
【发布时间】:2020-04-23 20:01:47
【问题描述】:

如何将带颜色的边框设置为UISegmentedControl

我默认得到这个(不正确):

我正在尝试这个,但没有任何反应

UISegmentedControl.appearance().layer.borderWidth = 1.0
UISegmentedControl.appearance().layer.cornerRadius = 5.0
UISegmentedControl.appearance().layer.borderColor = UIColor.white.cgColor
UISegmentedControl.appearance().layer.masksToBounds = true

我想实现这个边框(正确):

【问题讨论】:

  • 您在尝试该代码时得到了什么?
  • @koen 没有任何反应
  • 这并不意味着什么。能发正确和错误的图片吗?
  • @koen 请再次检查更新的问题
  • 这看起来像标准的UISegmentedControl。你是怎么设置控件的,可以加那个代码吗?

标签: ios swift iphone uikit


【解决方案1】:

我使用这个函数来改变我的 SegmentedControl:

func setSegmentedControlStyle(_ sgControl: UISegmentedControl, withColor: UIColor, normalTextColor: UIColor, withCornorRadius: CGFloat) {
        let sgcTitleAttributes = [NSAttributedString.Key.font: UIFont.nunitoSansRegularFontOfSize(15.0)!,
                                  NSAttributedString.Key.foregroundColor: normalTextColor] as [NSAttributedString.Key : Any]
        let sgcSelectedStateTitleAttributes = [NSAttributedString.Key.font: UIFont.nunitoSansRegularFontOfSize(15.0)!,
                                               NSAttributedString.Key.foregroundColor: _WHITE_COLOR] as [NSAttributedString.Key : Any]

    if #available(iOS 13.0, *) {
        sgControl.backgroundColor = UIColor.clear
        let tintColorImage = UIImage(color: .clear, size: CGSize(width: 1, height: sgControl.frame.height))
        let selectedTintColorImage = UIImage(color: withColor, size: CGSize(width: 1, height: sgControl.frame.height))
        sgControl.setBackgroundImage(tintColorImage, for: .normal, barMetrics: .default)
        sgControl.setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        sgControl.setBackgroundImage(selectedTintColorImage, for: .selected, barMetrics: .default)
        sgControl.selectedSegmentTintColor = withColor
    } else {
        sgControl.tintColor = withColor
    }
    sgControl.layer.cornerRadius = withCornorRadius
    sgControl.layer.borderWidth = 1.0
    sgControl.layer.borderColor = withColor.cgColor
    sgControl.layer.masksToBounds = true
    sgControl.setTitleTextAttributes(sgcTitleAttributes, for: .normal)
    sgControl.setTitleTextAttributes(sgcSelectedStateTitleAttributes, for: .selected)
}


extension UIImage {
    convenience init(color: UIColor, size: CGSize) {
        UIGraphicsBeginImageContextWithOptions(size, false, 1)
        color.set()
        let ctx = UIGraphicsGetCurrentContext()!
        ctx.fill(CGRect(origin: .zero, size: size))
        let image = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

        self.init(data: image.pngData()!)!
    }
}

您可以根据自己的需要四处玩耍并进行一些更改。

如果您有任何问题,请告诉我。

乐于助人!

【讨论】:

  • 这只会添加轮廓边框。是否可以添加内联边框以及提到的问题
  • 检查setDividerImage...创建彩色图像并在此处传递
猜你喜欢
  • 1970-01-01
  • 2010-10-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多