Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 将边框添加到UISegmentedControl_Ios_Swift_Iphone_Uikit - Fatal编程技术网

Ios 将边框添加到UISegmentedControl

Ios 将边框添加到UISegmentedControl,ios,swift,iphone,uikit,Ios,Swift,Iphone,Uikit,如何将带有颜色的边框设置为UISegmentedControl 默认情况下,我会得到以下信息(不正确): 我试过了,但什么也没发生 UISegmentedControl.appearance().layer.borderWidth=1.0 UISegmentedControl.appearance().layer.cornerRadius=5.0 UISegmentedControl.appearance().layer.borderColor=UIColor.white.cgColor UI

如何将带有颜色的边框设置为
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
我希望实现此边界(正确):


我使用此功能更改分段控件:

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()!)!
    }
}
你可以根据自己的需要进行调整

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


很乐意帮忙

当你尝试那个密码的时候,你得到了什么?@koen什么都没有发生,这并不意味着什么。您能发布正确和错误的图片吗?@koen请再次检查更新的问题,该问题看起来像是标准的
UISegmentedControl
。如何设置控件,是否可以添加该代码?这只会添加边框。是否可以添加内联边框以及前面提到的问题检查setDividerImage…创建彩色图像并将其传递到此处