【问题标题】:can not add a shadow to the view, CALayer无法在视图中添加阴影,CALayer
【发布时间】:2012-10-22 19:42:11
【问题描述】:

我正在通过关注为我的视图添加阴影

- (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;
}

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

我是不是在中途遗漏了一些要点。请就这个问题给我建议

【问题讨论】:

  • 我确实在Link Binary With Libraries 中添加了夸脱,我还在我的班级中添加了#import <QuartzCore/QuartzCore.h>..

标签: objective-c ios calayer shadow


【解决方案1】:

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

self.view.layer.masksToBounds = NO;
self.view.clipsToBounds = YES;

【讨论】:

  • 不,在现代,这两个属性是等价的,其中一个属性的变化会同步反映在另一个属性中。
【解决方案2】:

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

self.view.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);

【讨论】:

  • 我这样做 self.view.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); 因为我想在我的视野周围显示阴影..
【解决方案3】:

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

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

我所做的是显示view 的影子,而不是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;
}

【讨论】:

    【解决方案4】:

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

    【讨论】:

      【解决方案5】:

      你可以试试这个:

      -(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];
      }
      

      【讨论】:

        【解决方案6】:

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

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

        以便在您的视野中形成阴影。

        【讨论】:

          【解决方案7】:

          确保self.clipsToBounds = NO;

          【讨论】:

            【解决方案8】:

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

            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;
            

            【讨论】:

              猜你喜欢
              • 2015-09-19
              • 2020-04-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-08-11
              相关资源
              最近更新 更多