惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
B
Blog RSS Feed
宝玉的分享
宝玉的分享
腾讯CDC
博客园_首页
T
Tailwind CSS Blog
月光博客
月光博客
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
M
MIT News - Artificial intelligence
A
About on SuperTechFans
云风的 BLOG
云风的 BLOG
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
有赞技术团队
有赞技术团队
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
大猫的无限游戏
大猫的无限游戏
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
V
Visual Studio Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
SecWiki News
SecWiki News
美团技术团队
P
Privacy International News Feed
H
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Know Your Adversary
Know Your Adversary
Y
Y Combinator Blog
D
DataBreaches.Net
Project Zero
Project Zero
T
The Blog of Author Tim Ferriss
Cyberwarzone
Cyberwarzone
C
Cybersecurity and Infrastructure Security Agency CISA
C
Cisco Blogs
S
Schneier on Security
G
GRAHAM CLULEY
博客园 - 三生石上(FineUI控件)
Cisco Talos Blog
Cisco Talos Blog
小众软件
小众软件
Forbes - Security
Forbes - Security
D
Docker
T
Tenable Blog
S
Secure Thoughts
雷峰网
雷峰网
S
Security @ Cisco Blogs
T
The Exploit Database - CXSecurity.com
The Cloudflare Blog
博客园 - 【当耐特】
Spread Privacy
Spread Privacy
阮一峰的网络日志
阮一峰的网络日志

博客园 - chaoguo1234

如何自定义 UIScrollView 的减速系数 Plist 二进制格式 Any metadata 的内存布局 Mach-O 文件结构 Block Copy 的内存布局详解 __block 变量内存布局详解 Block 内存布局详解 XNU Inside: iOS 模拟器 WebKit Insie: WebKit 调试(二) NSMutableDictionary 的内存布局 NSDictionary 内存布局 WebKit Inside: GPU 进程调试 WebKit Inside: 渲染树 WebKit Inside: px 与 pt WebKit Inside: CSS 的匹配原理 dyld: 神秘的 __dso_handle 一文搞懂 ARM 64 系列: ADD(立即数版) 一文搞懂 ARM 64: AUTIBSP 一文搞懂 ARM 64 系列: PACISB XCode 编译 PAG 源码 一文搞懂 ARM 64 系列: 一文搞懂 ARM 64 系列: 函数调用传参与返回值 一文搞懂 ARM 64 系列: 寄存器
UIImageView 设置图片不生效的原因排查
chaoguo1234 · 2026-04-18 · via 博客园 - chaoguo1234

最近在项目中碰到了一个UIImageView的问题。

需求内容是无论是在浅色还是深色模式,UIImageView都使用深色模式下的图片。

最先想到的方法就是从Image Assets中按照下面方法取出对应的深色模式图片,然后赋值给UIImageViewimage属性:

image

var darkTrait = UITraitCollection(userInterfaceStyle: .dark)
// 取出暗黑模式下图片
let image = UIImage(named: name, in: nil, compatibleWith: darkTrait)
imageView.image = image

但是在浅色模式下,UIImageView还是显示浅色模式下的图片。

// 打印 image
(lldb) po imageView.image
▿ Optional<UIImage>
  - some : <UIImage:0x30ac59cc0 named(main: BDPPersonalPage_video_collection) {16, 16} renderingMode=automatic(original)>
  
// 打印 image 的 configuration
(lldb) expression -l objc -O -- [0x30ac59cc0 configuration]
0x0000000301439a00

// 打印 configuration 的内容
(lldb) expression -l objc -O -- 0x0000000301439a00
<UIImageConfiguration:0x301439a00 traits=(UserInterfaceIdiom = Pad, DisplayScale = 2, DisplayGamut = P3, HorizontalSizeClass = Regular, VerticalSizeClass = Regular, UserInterfaceStyle = Light, UserInterfaceLayoutDirection = LTR, PreferredContentSizeCategory = L, AccessibilityContrast = Normal)>

从上面imageView.imageconfiguration属性输出可以看到,图片的UserInterfaceStyle还是Light

通过调试UIImageViewsetImage:方法,发现内部会调用下面的方法:

-[UIImageView _resolveImagesWithPreviouslyDisplayedImage:]:

_resolveImagesWithPreviouslyDisplayedImage:方法会获取UIImageView当前的traitCollection:

0x19b688834 <+108>: bl     0x199d49170 ; -[UIImageView _effectiveImageViewTraitCollectionForResolvingImages]

lldb输出可以看到,UIImageViewUserInterfaceStyleLight:

(lldb) po $x0
<UITraitCollection: 0x17c7fa1c0; UserInterfaceIdiom = Pad, DisplayScale = 2, DisplayGamut = P3, HorizontalSizeClass = Regular, VerticalSizeClass = Regular, UserInterfaceStyle = Light, UserInterfaceLayoutDirection = LTR, ForceTouchCapability = Unavailable, PreferredContentSizeCategory = L, AccessibilityContrast = Normal, UserInterfaceLevel = Base, HDRHeadroomUsageLimit = 1, SceneCaptureState = 0, ImageDynamicRange = 1>

接着会比较UIImageViewtraitCollection和要设置的图片的traitCollection是否相等:

0x19b68884c <+132>: bl     0x19ad03818  ; -[UITraitCollection _isEqualToTraitCollectionForResolvingImage:]

只有两者相等,设置的图片才生效。

这里由于UIImageViewUserInterfaceStyleLight,而要设置的图片的UserInterfaceStyleDark,因此二者不相等。

在二者不相等的情况下,UIImageView会使用自己的traitCollectionImage Assets中找对应的图片:

 0x19b68898c <+452>: bl     0x19b68858c  ; -[UIImageView _resolvedImageFromImage:withImageViewTrait:]

由于UIImageViewUserInterfaceStyleLight,因此找到的也是Light图片。

最后UIImageView会将找到的图片设置为真正要显示的图片。

这就是为什么使用暗黑模式的图片设置UIImageViewimage属性不成功的原因。

知道了原因,想要解决,就只需要将保证UIImageViewUserInterfaceStyle和要设置的图片一样即可。

imageView.overrideUserInterfaceStyle = .dark

加上上面代码,UIImageView就可以正常显示暗黑模式的图片了。

实际上,设置了UIImageViewoverrideUserInterfaceStyle属性为dark,即使不设置UIImageViewimage属性为暗黑模式图片,系统也会自动切换到暗黑模式图片。