CALayer 在亮/暗模式下显示错误的 SFSymbol 图像

问题描述

我的第一个 SO 问题,所以这里是...

我非常熟悉使用 CALayer 及其变体,但这个让我很难过。

让我们从代码开始(是的,我在使用 Objective-C)...

- (void)setup
{
    // a basic setup method,called from layoutSubviews
    
    if( self.needsSetup )
    {
        self.needsSetup = NO;
        
        if( !_infoTextLayer )
        {
            
            _infoTextLayer = [self makeLowerTextLayer];
            
            [self.layer addSublayer: _infoTextLayer];
            
            
            CGFloat midX,midY;
            
            midX = CGRectGetMidX( self.layer.bounds );
            midY = CGRectGetMidY( self.layer.bounds );
            
            CGRect  itlBounds   = _infoTextLayer.bounds; // our reference for size
            
            CGRect  frameRect   = CGRectMake( 0.0f,0.0f,itlBounds.size.height,itlBounds.size.height);
            CGPoint pos         = CGPointMake( midX,midY );

            CALayer *newLayer   = [self makeBasicLayer: @"infoSymbol" : frameRect : pos];
            
            newLayer.delegate   = self;

            _infoLayer          = newLayer;
            
            [_infoLayer setNeedsdisplay];
            
            [self.layer addSublayer: newLayer];
            
        }
    }
}

// a simple utility method...
- (CALayer *)makeBasicLayer:  (Nsstring *) name :(CGRect) frame : (CGPoint) position
{
    CALayer *newLayer = [CALayer layer];
    
    newLayer.rasterizationScale = [[UIScreen mainScreen] scale];
    newLayer.contentsScale      = [[UIScreen mainScreen] scale];
    
    newLayer.name               = name;
    newLayer.frame              = frame;
    newLayer.position           = position;
    newLayer.contentsGravity    = kCAGravityResize;
    
    return newLayer;
}

// delegate method
- (void)displayLayer:(CALayer *)layer
{
    if( [layer.name isEqualToString: @"infoSymbol"] ) // make sure we're changing the correct layer
    {
        uitraitcollection   *tc = self.traitCollection;
        
        // just to give us a little more visual Feedback...
        if( tc.userInterfaceStyle == UIUserInterfaceStyleDark )
        {
            layer.backgroundColor = [UIColor systemYellowColor].CGColor;
        }
        else
        {
            layer.backgroundColor = [UIColor whiteColor].CGColor;

        }
        
        // normally I'd set the layers contents like this...
        //layer.contents  = CFBridgingrelease([UIImage systemImageNamed: @"info.circle" compatibleWithTraitCollection: tc].CGImage);
        
        // but for the sake of this question to SO...
        UIImage *sysImage   = [UIImage systemImageNamed: @"info.circle" compatibleWithTraitCollection: tc];
        
        layer.contents  = CFBridgingrelease( sysImage.CGImage );
        

        // doing this will return the system image with the white color but when it is displayed by
        // the CALayer the on screen image is a black ( default UIUserInterfaceStyleLight ) image
//        layer.contents          = CFBridgingrelease([[UIImage systemImageNamed: @"info.circle"] imageWithTintColor: [UIColor whiteColor]].CGImage);
//
    }
}

在 traitCollectionDidChange 中,我再次调用 [self.infoLayer setNeedsdisplay]; 来触发委托方法 displayLayer,该方法按预期调用,但返回的图像始终是光照模式的图像。

我在模拟器或真实设备(iPhone 或 iPad)上运行时遇到了相同的天气行为。

这些是模拟器的截图(11 Pro,iOS 14.5)

light mode screen shot

dark mode screen shot

在这里遗漏了什么吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)