2012-03-04 103 views
0

我有以下代碼:添加陰影UINavigationBar的底部只有

self.navigationBar_.layer.shadowColor = [UIColor blackColor].CGColor; 
      self.navigationBar_.layer.shadowOpacity = 0.3f; 
      self.navigationBar_.layer.shadowOffset = CGSizeMake(0.0f, 0.0f); 
      self.navigationBar_.layer.shadowRadius = 3.0f; 
      self.navigationBar_.layer.masksToBounds = NO; 

我基本上只希望邊框添加到底部,而不是整個矩形。我該怎麼做呢?上面的代碼也會爲左邊,右邊,上邊框添加陰影。

回答

4

您可以使用漸變來代替圖層陰影。你可以使用透明的PNG漸變,但這裏是一個如何編程的例子:

UIView *topShadowView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.navigationBar.bounds.size.width, 10)]; 
CAGradientLayer *topShadow = [CAGradientLayer layer]; 
topShadow.frame = CGRectMake(0, 0, self.navigationBar.bounds.size.width, 10); 
topShadow.colors = [NSArray arrayWithObjects:(id)[[UIColor colorWithWhite:0.0 alpha:0.25f] CGColor], (id)[[UIColor clearColor] CGColor], nil]; 
[topShadowView.layer insertSublayer:topShadow atIndex:0]; 
[self.view addSubview:topShadowView];