有人知道用resizableImageWithCapInsets拉伸的UIImage可以响应亮/暗模式下的更改吗?我当前的实现仅在第一次绘制时考虑暗/亮模式。
[thumbnailContainer addSubview:[self addTileBackgroundOfSize:thumbnailContainer.bounds]];
- (UIImageView *) addTileBackgroundOfSize:(CGRect)bounds {
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:bounds];
UIEdgeInsets insets = UIEdgeInsetsMake(10.0f, 49.0f, 49.0f, 10.0f);
UIImage *backgroundImage = [[UIImage imageNamed:@"UnivGalleryTile"] resizableImageWithCapInsets:insets];
backgroundView.image = backgroundImage;
return backgroundView;
}
我想我可以用traitCollection委托方法重绘它们,但我希望有一种更好的方法来使它们响应。
答案 0 :(得分:0)
似乎resizableImageWithCapsInsets
会导致图像丢失其动态,自动适应的属性。您可能会尝试为两种外观创建图像,然后将它们重新组合为动态图像。查看this gist上的操作方法。
答案 1 :(得分:0)
首先,这并不奇怪。当您说resizableImage
时,您将制作一个新图像。不再是您从资产目录中获得的图像,因此它失去了自动链接/动态性,当特征集合更改时,该自动链接/动态性使图像自动更改为另一个图像。
第二,没关系,因为您可以创建与任何两个图像(不在资产目录中)的链接。您可以通过UIImageAsset类来实现。
这是一个可行的示例。假设Faces是资产目录中一对的名称,一个代表Any,一个代表Dark。我将提取该对中的每个成员,将resizable
应用于每个对,然后将 new 对作为彼此的变体连接在一起:
let tclight = UITraitCollection(userInterfaceStyle: .light)
let tcdark = UITraitCollection(userInterfaceStyle: .dark)
var smiley = UIImage(named: "Faces", in: nil, compatibleWith: tclight)!
var frowney = UIImage(named: "Faces", in: nil, compatibleWith: tcdark)!
let link = UIImageAsset()
let insets = UIEdgeInsets(top: 30, left: 30, bottom: 30, right: 30)
smiley = smiley.resizableImage(withCapInsets: insets)
frowney = frowney.resizableImage(withCapInsets: insets)
link.register(smiley, with: tclight)
link.register(frowney, with: tcdark)
或者在Objective-C中:
UITraitCollection* tclight = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleLight];
UITraitCollection* tcdark = [UITraitCollection traitCollectionWithUserInterfaceStyle:UIUserInterfaceStyleDark];
UIImage* smiley = [UIImage imageNamed:@"Faces" inBundle:nil compatibleWithTraitCollection:tclight];
UIImage* frowney = [UIImage imageNamed:@"Faces" inBundle:nil compatibleWithTraitCollection:tcdark];
UIImageAsset* link = [UIImageAsset new];
UIEdgeInsets insets = UIEdgeInsetsMake(30, 30, 30, 30);
smiley = [smiley resizableImageWithCapInsets:insets];
frowney = [frowney resizableImageWithCapInsets:insets];
[link registerImage:smiley withTraitCollection:tclight];
[link registerImage:frowney withTraitCollection:tcdark];
全部完成。请注意,在代码中不需要保留任何对象(link
,smiley
,frowney
)。
现在,如果您将一对中的一个插入图像视图中,则当用户亮/暗模式更改时,它将自动更改为另一个:
let tc = self.traitCollection
let im = link.image(with: tc)
self.imageView.image = im
我将在明暗模式之间来回切换,以证明这是可行的:
答案 2 :(得分:-1)
我已经解决了,但是男孩真丑。因此,如果有人有更好的解决方案,我就会接受:
我首先将图像视图存储在NSMutableArray中:
0.0.0.0:8000
然后当用户更改屏幕模式时,我手动重置背景图像:
- (UIImageView *) addTileBackgroundOfSize:(CGRect)bounds {
UIImageView *backgroundView = [[UIImageView alloc] initWithFrame:bounds];
UIEdgeInsets insets = UIEdgeInsetsMake(10.0f, 49.0f, 49.0f, 10.0f);
UIImage *backgroundImage = [[UIImage imageNamed:@"UnivGalleryTile"] resizableImageWithCapInsets:insets];
backgroundView.image = backgroundImage;
// Store image for re-drawing upon dark/light mode change
[thumbnailArray addObject:backgroundView];
return backgroundView;
}