我的投影重新掠过自己?

时间:2013-02-19 16:28:51

标签: iphone ios5 ios6 uinavigationcontroller uinavigationbar

我已经为UINavigationController创建子类,在方法viewDidApper上添加了一个投影:来自UINavigationController,如下所示:

    - (void)viewDidAppear:(BOOL)animated
{
    // Add shadow to Navigation Bar
    CAGradientLayer *newShadow = [[CAGradientLayer alloc] init];
    newShadow.shadowColor = [[UIColor blackColor] CGColor];
    newShadow.shadowOffset = CGSizeMake(0.0f, 4.0f);
    newShadow.shadowOpacity = 1.0f;
    CGRect shadowPath = CGRectMake(self.navigationBar.layer.bounds.origin.x - 10, self.navigationBar.layer.bounds.size.height - 6, self.navigationBar.layer.bounds.size.width + 20, 5);
    newShadow.shadowPath = [UIBezierPath bezierPathWithRect:shadowPath].CGPath;
    newShadow.shouldRasterize = YES;
    self.navigationBar.clipsToBounds = NO;
    [self.navigationBar.layer addSublayer:newShadow];
    [super viewDidAppear:animated];
}

当我在ModalViewController中使用此NavigationController时,阴影会一遍又一遍地重新绘制。

1 个答案:

答案 0 :(得分:2)

因为每次出现视图时都会调用- viewDidAppear:方法。您可能希望将此代码移到- viewDidLoad方法中。