gpt4 book ai didi

iOS:当 View 背景颜色为清晰颜色时,图层背景颜色不显示

转载 作者:可可西里 更新时间:2023-11-01 05:02:01 25 4
gpt4 key购买 nike

我有两个兄弟 View :一个灰色标签和一个绿色按钮,按钮在下方。出于某种原因,我需要将 label.backgroundColor 设置为清除颜色并将 label.layer.backgroundColor 设置为灰色。按钮颜色为绿色。我希望在屏幕上看到灰色(因为标签在按钮的顶部)。但我只看到绿色(按钮的颜色)。为什么?

编辑:相关代码

// in my custom cell
-(void)awakeFromNib
{
[super awakeFromNib];

// customize label
_label.layer.cornerRadius = 5;
_label.layer.backgroundColor = [UIColor grayColor].CGColor;
_label.backgroundColor = [UIColor clearColor];
_label.layer.masksToBounds = NO;


// customize button
// show shadow and rounded corner at the same time

_button.backgroundColor = [UIColor clearColor];
_button.layer.backgroundColor = [UIColor greenColor].CGColor;
_button.layer.masksToBounds = NO;
_button.layer.cornerRadius = 10.0f;

self.layer.masksToBounds = NO;
self.layer.cornerRadius = 10.0f;
self.layer.shadowOpacity = 0.5f;
self.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:_button.bounds cornerRadius:10.0f].CGPath;
self.layer.shadowOffset = CGSizeMake(0.0f, 4.0f);
self.layer.shadowRadius = 2.0f;

}

最佳答案

最后我找到了解决方案:只需将这两行的顺序颠倒即可:

// both set to clear color eventually
_label.layer.backgroundColor = [UIColor grayColor].CGColor;
_label.backgroundColor = [UIColor clearColor];

为此:

// both set to gray eventually
_label.backgroundColor = [UIColor clearColor];
_label.layer.backgroundColor = [UIColor grayColor].CGColor;

原因是对于 UILabel(虽然不是 UIView,稍后会详细介绍)设置其背景颜色会将其层的背景颜色设置为 nil。但是如果我们先设置标签背景色,然后再设置图层背景色,然后两者各行其是,那么我们就会在屏幕上看到标签背景色。

但对于 UIView 来说,情况就完全不同了:view 的背景色和它的 layer 的背景色是一回事。因此,以最后一组为准。

但是,我在文档中找不到关于上述观察的任何内容。希望有人能介入并提供帮助。

关于iOS:当 View 背景颜色为清晰颜色时,图层背景颜色不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15597956/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com