UISegmentedControl iOS 13 clear color(UISegmentedControl iOS 13 清除颜色)
问题描述
在 iOS 12 上,要获得具有清晰边框、清晰分隔线的 UISegmentedControl,一切都很容易.我所做的只是:
On iOS 12, to get a UISegmentedControl with clear border, clear divider line, everything clear was easy. All I did was this:
  settingControl.tintColor = .clear
   let font = myFont
   let boldfont = myBoldFont
  settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.white, NSAttributedString.Key.font:font], for: .normal)
  settingControl.setTitleTextAttributes([NSAttributedString.Key.foregroundColor : UIColor.red, NSAttributedString.Key.font:boldfont], for: .selected)
然后 UISegmentedControl 是完全清晰的颜色(分隔线、背景、边框)
And then UISegmentedControl was full clear color (divider line, background, border)
但在 iOS 13 中,我无法完全清楚.我可以设置
But in iOS 13, I can not get it fully clear. I can set
settingControl.selectedSegmentTintColor = UIColor.clear
但它仍然没有清除背景颜色和分隔线.
But it still does not clear the backgroundColor and divider line.
我尝试将 backgroundColor 设置为清除,但没有效果.
I tried setting backgroundColor to clear, but no effect.
 settingControl.backgroundColor = UIColor.clear
我也尝试设置清晰的图像,但仍然没有:
I also tried setting a clear image but still nothing:
 public extension UIImage {
  /**
   Returns image with size 1x1px of certain color.
   */
 class func imageWithColor(color : UIColor) -> UIImage? {
    let rect = CGRect(x: 0, y: 0, width: 1, height: 1)
    UIGraphicsBeginImageContext(rect.size)
    let context = UIGraphicsGetCurrentContext()
    context?.setFillColor(color.cgColor)
    context?.fill(rect)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image
}
 }
然后是这个:
  let clearImage = UIImage.imageWithColor(color: UIColor.clear)
    settingControl.setDividerImage(clearImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
   settingControl.setBackgroundImage(clearImage, for: .normal, barMetrics: .default)
   settingControl.setBackgroundImage(clearImage, for: .selected, barMetrics: .default)
这就是它在 iOS 12 上的样子.在 iOS 13 上,这似乎是不可能的.
This is how it looks like on iOS 12. On iOS 13, this seems impossible.
推荐答案
下面是一种在 iOS 13 中复制普通分段控件的方法:
Here's a way to replicate that plain segmented control in iOS 13:
import UIKit
import PlaygroundSupport
class PlainSegmentedControl: UISegmentedControl {
    override init(items: [Any]?) {
        super.init(items: items)
        setup()
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // Used for the unselected labels
    override var tintColor: UIColor! {
        didSet {
            setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
        }
    }
    // Used for the selected label
    override var selectedSegmentTintColor: UIColor? {
        didSet {
            setTitleTextAttributes([.foregroundColor: selectedSegmentTintColor ?? tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
        }
    }
    private func setup() {
        backgroundColor = .clear
        // Use a clear image for the background and the dividers
        let tintColorImage = UIImage(color: .clear, size: CGSize(width: 1, height: 32))
        setBackgroundImage(tintColorImage, for: .normal, barMetrics: .default)
        setDividerImage(tintColorImage, forLeftSegmentState: .normal, rightSegmentState: .normal, barMetrics: .default)
        // Set some default label colors
        setTitleTextAttributes([.foregroundColor: UIColor.black, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .normal)
        setTitleTextAttributes([.foregroundColor: tintColor!, NSAttributedString.Key.font: UIFont.systemFont(ofSize: 13, weight: .regular)], for: .selected)
    }
}
这里有一些测试代码可以放在 Playground 中:
Here's some test code to put in a playground:
// Create a dark green view as a test background
let bg = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100))
bg.backgroundColor = UIColor(red: 0.224, green: 0.408, blue: 0.467, alpha: 1)
// The plain segmented control
let seg = PlainSegmentedControl(items: ["Number One", "Number Two", "Number Three"])
seg.tintColor = .white
seg.selectedSegmentTintColor = .green
seg.selectedSegmentIndex = 0
bg.addSubview(seg)
PlaygroundPage.current.liveView = bg
这是 UIImage 扩展,用于从颜色创建大小图像:
Here's the UIImage extension to create a sized image from a color:
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()!)!
    }
}
                        这篇关于UISegmentedControl iOS 13 清除颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:UISegmentedControl iOS 13 清除颜色
				
        
 
            
        - MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
 - 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
 - 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
 - Android - 拆分 Drawable 2022-01-01
 - Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
 - android 4中的android RadioButton问题 2022-01-01
 - 用 Swift 实现 UITextFieldDelegate 2022-01-01
 - 想使用ViewPager,无法识别android.support.*? 2022-01-01
 - Android viewpager检测滑动超出范围 2022-01-01
 - 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
 
				
				
				
				