reason: 'UIView is missing its initial trait collection populated during initialization. This is ...

运行环境:iPhone11真机,iOS15

控制台错误信息如下:
Trapped uncaught exception 'NSInternalInconsistencyException', reason: 'UIView is missing its initial trait collection populated during initialization. This is a serious bug, likely caused by accessing properties or methods on the view before calling a UIView initializer.

这是由于在调用UIView初始值设定项之前访问视图上的属性或方法引起的。也就是init之前就设置backgroundColor是不被允许的(iOS15之前,这样运行不会报错)。

解决办法:backgroundColor这行代码放到initWithFrame之后就可以。

你可能感兴趣的:(reason: 'UIView is missing its initial trait collection populated during initialization. This is ...)