3

我正在通过以下方式为我的视图添加阴影

- (void)viewDidLoad{
    [super viewDidLoad];

    self.view.layer.shadowColor = [UIColor blackColor].CGColor;
    self.view.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    self.view.layer.shadowOpacity = 1.0f;
    self.view.layer.shadowRadius = 4.0f;
    self.view.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
}

但是,我得到一个完全没有阴影的视图,如下所示

在此处输入图像描述

我是否在中途错过了一些要点。请就这个问题给我建议

4

8 回答 8

3

您需要将 layer.masksToBounds 设置为 NO 并将 clipsToBounds 设置为 YES。

self.view.layer.masksToBounds = NO;
self.view.clipsToBounds = YES;
于 2014-10-09T14:14:10.803 回答
1

使偏移量为 0,将对视图产生 nil 效果。

于 2012-10-23T03:48:38.973 回答
1

对不起大家,这是我的愚蠢错误。我拥有的视图结构是

view(UIView) ( in white color )
    |
    |
    aView (UIView) ( in orange color )

我所做的就是展示viewnot的影子aView。刚刚更正了如下代码

(void)viewDidLoad{
    [super viewDidLoad];

    self.aView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.aView.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
    self.aView.layer.shadowOpacity = 1.0f;
    self.aView.layer.shadowRadius = 4.0f;
    self.aView.layer.shadowPath = [UIBezierPath bezierPathWithRect:self.view.bounds].CGPath;
}
于 2012-10-22T20:09:36.443 回答
1

你根本没有抵消你的影子。尝试:

self.view.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
于 2012-10-22T19:50:49.063 回答
0

您应该使偏移量具有一定的值而不是 0。例如:

self.view.layer.shadowOffset = CGSizeMake(3, -1);

这样您的视野中就会形成阴影。

于 2012-10-23T03:45:37.993 回答
0

你可以试试这个:

-(void)makeShadow
{
      self.layer.shadowOpacity = 0.7f;
      self.layer.shadowOffset = CGSizeMake(5.0f,3.0f);
      self.layer.shadowColor =[[UIColor blackColor] CGColor];
//    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
//    layer.shadowPath = path.CGPath;
//    layer.shadowRadius = 5.0f;
}
-(void)makeCornerRadius:(CGFloat)_cornerRadius
{
   self.cornerRadius = _cornerRadius;
   [self.layer setMasksToBounds:NO];
   [self.layer setCornerRadius:_cornerRadius];
}
于 2012-10-23T06:57:42.163 回答
0

确保 self.clipsToBounds = NO;

于 2015-07-09T03:34:45.490 回答
0

您的视图看起来与您提供的代码无关。确保您将阴影应用到正确的视图,您没有将视图遮盖到其边界,并且颜色正确。下面的代码显示了一个工作示例:

UIView *pointView = [[UIView alloc] initWithFrame:pointLabel.frame];
pointView.backgroundColor = [UIColor whiteColor];
pointView.layer.shadowColor = [UIColor lightGrayColor].CGColor;
pointView.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
pointView.layer.shadowOpacity = 1.0f;
pointView.layer.shadowRadius = 5.0f;
pointView.layer.shadowPath = [UIBezierPath bezierPathWithRect:pointView.bounds].CGPath;
于 2017-04-23T05:51:55.443 回答