如何为UINavigation栏添加阴影效果

时间:2015-12-08 09:27:41

标签: ios uinavigationbar quartz-core dropshadow

enter image description here

您好,我想为我的NAvigationBar添加这种阴影我该怎么做。

这是我尝试添加阴影的方式。

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage=[UIImage new];

self.navigationController.navigationBar.translucent=YES;

self.navigationController.navigationBar.topItem.titleView.tintColor=[UIColor whiteColor];
self.navigationController.navigationBar.titleTextAttributes=[NSDictionary dictionaryWithObject:[UIFont fontWithName:@"HelveticaNeue" size:15.0f] forKey:NSFontAttributeName];
self.navigationController.navigationBar.topItem.title=strNavigtionTitle;
self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backarrow"] style:UIBarButtonItemStylePlain target:self action:@selector(revealToggle :)];
[self.navigationController navigationBar].tintColor = [UIColor whiteColor];

[self.navigationController navigationBar].layer.shadowColor=[UIColor colorWithRed:53.0/255.0 green:108.0/255.0 blue:130.0/255.0 alpha:1.0f].CGColor;
[self.navigationController navigationBar].layer.shadowOffset=CGSizeMake(0, 20);
[self.navigationController navigationBar].layer.shadowOpacity=0.8;
[self.navigationController navigationBar].layer.shadowRadius=5.5;

但这只会为箭头和我的申请休假标题添加阴影。但是我想在这张图片中添加一个投影。它应该在NavigationBar和我的主UIView之间。我该怎么做?请帮我。 感谢

3 个答案:

答案 0 :(得分:14)

在这里,您需要导入QuartzCore框架。

self.navigationController.navigationBar.layer.borderColor = [[UIColor whiteColor] CGColor]; self.navigationController.navigationBar.layer.borderWidth=2;// set border you can see the shadow 
self.navigationController.navigationBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(1.0f, 1.0f);
self.navigationController.navigationBar.layer.shadowRadius = 3.0f;
self.navigationController.navigationBar.layer.shadowOpacity = 1.0f;
self.navigationController.navigationBar.layer.masksToBounds=NO;

另一件事 你必须

set self.layer.masksToBounds = NO;

此属性的默认值为YES,这意味着即使渲染阴影,也不会在视图边界外渲染,这意味着您无法在视图边界看到它所有

如果您以任何方式为此视图设置动画,则还应添加以下行:

self.layer.shouldRasterize = YES;

答案 1 :(得分:9)

self.navigationController.navigationBar.layer.shadowColor = [[UIColor blackColor] CGColor];
self.navigationController.navigationBar.layer.shadowOffset = CGSizeMake(2.0f, 2.0f);
self.navigationController.navigationBar.layer.shadowRadius = 4.0f;
self.navigationController.navigationBar.layer.shadowOpacity = 1.0f;

答案 2 :(得分:3)

我可以通过这种方式实现这一目标。我删除了向导航栏添加阴影。而不是我在导航栏下放置相同大小的视图。将其背景颜色设置为导航栏颜色。然后为该视图添加了阴影。这非常有效。

-(void)setupNavigationBar
{
    [self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage=[UIImage new];

    self.navigationController.navigationBar.translucent=YES;
    self.navigationController.navigationBar.topItem.titleView.tintColor=[UIColor whiteColor];
    self.navigationController.navigationBar.titleTextAttributes=[NSDictionary dictionaryWithObject:[UIFont fontWithName:@"HelveticaNeue" size:15.0f] forKey:NSFontAttributeName];
    self.navigationController.navigationBar.topItem.title=strNavigtionTitle;
    self.navigationController.navigationBar.titleTextAttributes = [NSDictionary dictionaryWithObject:[UIColor whiteColor] forKey:NSForegroundColorAttributeName];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backarrow"] style:UIBarButtonItemStylePlain target:self action:@selector(revealToggle :)];
    [self.navigationController navigationBar].tintColor = [UIColor whiteColor];

    UIView *shadow=[[UIView alloc] initWithFrame:CGRectMake(0, 0, dm.screenWidth, 64)];
    [shadow setBackgroundColor:[UIColor colorWithRed:62.0/255.0 green:81.0/255.0 blue:119.0/255.0 alpha:1.0]];
    shadow.layer.shadowColor=[UIColor colorWithRed:51/255 green:76/255 blue:104/255 alpha:1.0].CGColor;
    shadow.layer.shadowOffset=CGSizeMake(0, 15);
    shadow.layer.shadowOpacity=0.12;
    shadow.layer.shadowRadius=4.5;
    [self.view addSubview:shadow];
}