1

我的第一个 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)

灯光模式截图

黑暗模式屏幕截图

我在这里错过了什么吗?

4

0 回答 0