gpt4 book ai didi

ios - 更改应用程序主题时 UISegmentedControl 没有更改?

转载 作者:行者123 更新时间:2023-11-28 07:42:42 25 4
gpt4 key购买 nike

我正在尝试为应用创建 2 个主题 - 浅色和深色。

在它们之间切换时,UISegmentedControl 不会被更改。它保持原始状态。不知道为什么。我确定我遗漏了什么或做错了什么,我只是想不通是什么。

class DiscoverVC: UIViewController {

@IBOutlet weak var segmentedControl: UISegmentedControl!

override func viewDidLoad() {
super.viewDidLoad()
segmentedControl.addUnderlineForSelectedSegment()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
applyTheme()
}
}

extension DiscoverVC {

fileprivate func applyTheme() {
segmentedControl.tintColor = Theme.current.tint
}
}



extension UISegmentedControl{

func removeBorder() {
let backgroundImage = UIImage.getColoredRectImageWith(color: UIColor.clear.cgColor, andSize: self.bounds.size)
self.setBackgroundImage(backgroundImage, for: .normal, barMetrics: .default)
self.setBackgroundImage(backgroundImage, for: .selected, barMetrics: .default)
self.setBackgroundImage(backgroundImage, for: .highlighted, barMetrics: .default)

let deviderImage = UIImage.getColoredRectImageWith(color: UIColor.clear.cgColor, andSize: CGSize(width: 1.0, height: self.bounds.size.height))
self.setDividerImage(deviderImage, forLeftSegmentState: .selected, rightSegmentState: .normal, barMetrics: .default)
self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: Theme.current.segmentedControl_unselectedColor, NSAttributedStringKey.font: UIFont(name: Fonts.OpenSans_Regular, size: 13)!], for: .normal)
self.setTitleTextAttributes([NSAttributedStringKey.foregroundColor: Theme.current.segmentedControl_tintColor, NSAttributedStringKey.font: UIFont(name: Fonts.OpenSans_Bold, size: 13)!], for: .selected)
}

func addUnderlineForSelectedSegment() {
removeBorder()
let underlineWidth: CGFloat = self.bounds.size.width / CGFloat(self.numberOfSegments)
let underlineHeight: CGFloat = 2.0
let underlineXPosition = CGFloat(selectedSegmentIndex * Int(underlineWidth))
let underLineYPosition = self.bounds.size.height - 1.0
let underlineFrame = CGRect(x: underlineXPosition, y: underLineYPosition, width: underlineWidth, height: underlineHeight)
let underline = UIView(frame: underlineFrame)
underline.backgroundColor = Theme.current.segmentedControl_tintColor
underline.tag = 1
self.addSubview(underline)
}

func changeUnderlinePosition() {
guard let underline = self.viewWithTag(1) else {return}
let underlineFinalXPosition = (self.bounds.width / CGFloat(self.numberOfSegments)) * CGFloat(selectedSegmentIndex)
UIView.animate(withDuration: 0.1, animations: {
underline.frame.origin.x = underlineFinalXPosition
})
}
}

最佳答案

在这个 removeBorder 函数中,您要从 SegmentedControl 中删除所有内容(边框、文本颜色、分隔符等)

那么设置tint color会改变什么颜色呢?那里有一切的图片。

我不确定这是否是唯一的原因。您应该尝试对此发表评论并检查色调颜色是否适用。

func addUnderlineForSelectedSegment() {
//removeBorder()
YOUR Existing CODE HERE
}

尝试一下,如果这是原因。

此外,请确认您是否从 Theme.current.tint 获取颜色值。

关于ios - 更改应用程序主题时 UISegmentedControl 没有更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51784501/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com