Colors 深色模式中的颜色不符合预期
我想创建一个对暗模式有反应的形状,所以颜色在暗模式下应该是白色,在正常模式下应该是黑色 这是我的代码:(可复制且可运行) 结果->参见图片 我希望如果我将strokecolor设置为UIColor.label.cgColor,它将采用“正确”的颜色,但事实并非如此。我做错了什么Colors 深色模式中的颜色不符合预期,colors,swiftui,ios-darkmode,Colors,Swiftui,Ios Darkmode,我想创建一个对暗模式有反应的形状,所以颜色在暗模式下应该是白色,在正常模式下应该是黑色 这是我的代码:(可复制且可运行) 结果->参见图片 我希望如果我将strokecolor设置为UIColor.label.cgColor,它将采用“正确”的颜色,但事实并非如此。我做错了什么 import SwiftUI import UIKit @available(iOS 13.0, *) public struct DarkView<Content> : View where Conten
import SwiftUI
import UIKit
@available(iOS 13.0, *)
public struct DarkView<Content> : View where Content : View {
var darkContent: Content
var on: Bool
public init(_ on: Bool, @ViewBuilder content: () -> Content) {
self.darkContent = content()
self.on = on
}
public var body: some View {
ZStack {
if on {
Spacer()
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
.background(Color.black)
.edgesIgnoringSafeArea(.all)
darkContent.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity).background(Color.black).colorScheme(.dark)
} else {
darkContent
}
}
}
}
@available(iOS 13.0, *)
extension View {
public func darkModeFix(_ on: Bool = true) -> DarkView<Self> {
DarkView(on) {
self
}
}
}
class ShapeView : UIView {
let shapeLayer = CAShapeLayer()
init() {
super.init(frame: CGRect.zero)
shapeLayer.strokeColor = UIColor.label.cgColor
shapeLayer.path = UIBezierPath(arcCenter: CGPoint(x: 0, y: 0), radius: 100, startAngle: 0, endAngle: CGFloat(Double.pi), clockwise: true).cgPath
self.layer.addSublayer(shapeLayer)
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
@available(iOS 13.0, *)
struct ShapeWrapper: UIViewRepresentable {
func makeUIView(context: UIViewRepresentableContext<ShapeWrapper>) -> ShapeView {
let view = ShapeView()
return view
}
func updateUIView(_ uiView: ShapeView, context:
UIViewRepresentableContext<ShapeWrapper>) {
}
}
struct ContentView: View {
var body: some View {
VStack {
Text("Hello, World!")
ShapeWrapper()
.fixedSize()
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
struct ContentViewDarkmode_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environment(\.colorScheme, .dark)
.darkModeFix()
}
}
导入快捷界面
导入UIKit
@可用(iOS 13.0,*)
公共结构DarkView:视图,其中内容:视图{
var darkContent:Content
var on:Bool
public init(uon:Bool,@ViewBuilder content:()->content){
self.darkContent=content()
self.on=on
}
公共机构:一些看法{
ZStack{
如果打开{
垫片()
.frame(最小宽度:0,最大宽度:。无穷大,最小高度:0,最大高度:。无穷大)
.背景(颜色.黑色)
.edgesIgnoringSafeArea(.all)
darkContent.frame(minWidth:0,maxWidth:.infinity,minHeight:0,maxHeight:.infinity)。背景(Color.black)。配色方案(.dark)
}否则{
暗色内容
}
}
}
}
@可用(iOS 13.0,*)
扩展视图{
public func darkModeFix(on:Bool=true)->DarkView{
DarkView(开){
自己
}
}
}
类ShapeView:UIView{
设shapeLayer=CAShapeLayer()
init(){
super.init(帧:CGRect.zero)
shapeLayer.strokeColor=UIColor.label.cgColor
shapeLayer.path=UIBezierPath(弧心:CGPoint(x:0,y:0),半径:100,星形缠结:0,端角:CGFloat(Double.pi),顺时针:true)。cgPath
self.layer.addSublayer(shapeLayer)
}
必需初始化?(编码器:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
}
@可用(iOS 13.0,*)
结构ShapeWrapper:UIViewRepresentable{
func makeUIView(上下文:UIViewRepresentableContext)->ShapeView{
let view=ShapeView()
返回视图
}
func updateUIView(uiView:ShapeView,context:
UIViewRepresentableContext){
}
}
结构ContentView:View{
var body:一些观点{
VStack{
文本(“你好,世界!”)
ShapeWrapper()
.fixedSize()
}
}
}
结构内容视图\u预览:PreviewProvider{
静态var预览:一些视图{
ContentView()
}
}
结构ContentViewDarkmode_预览:PreviewProvider{
静态var预览:一些视图{
ContentView()
.environment(\.colorScheme,.dark)
.darkModeFix()
}
}
这是预览版。在模拟器上运行良好。使用Xcode 11.2/iOS 13.2.OMG进行测试……谢谢…@Asperi预览背景错误有效,而不是他的代码