如何解决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)
我在这里遗漏了什么吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。