How to add shadow to UINavigationController

左心房为你撑大大i 提交于 2020-01-04 11:11:13

问题


I want to create a Slide Menu like in Path or Facebook with a shadow between my menu and the UINavigationController with all the content inside. I can add rounded corners to the layer but the shadow I apply to the layer is not shown. What is wrong with that code? It is working if I use a UIViewController instead of the UINavigationController... (The code is inside a view added as rootViewController to the UINavigationController)

The problem is that I don't want to loose the rounded borders of the UINavigationController

self.navigationController.view.layer.shadowPath = [UIBezierPath bezierPathWithRoundedRect:self.navigationController.view.bounds cornerRadius:self.navigationController.view.layer.cornerRadius].CGPath;
[self.navigationController.view.layer setMasksToBounds:YES];
[self.navigationController.view.layer setShadowColor:[UIColor blackColor].CGColor];
[self.navigationController.view.layer setShadowOffset:CGSizeMake(0, 0)];
[self.navigationController.view.layer setShadowOpacity:0.5];
[self.navigationController.view.layer setShadowRadius:3];
[self.navigationController.view.layer setCornerRadius:3];

回答1:


In Swift3

self.navigationController?.view.layer.shadowOffset = CGSize(width: 0, height: 5)
self.navigationController?.view.layer.masksToBounds = true



回答2:


The problem is

[self.navigationController.view.layer setMasksToBounds:YES];

As the name implies, everything outside the layer - including the shadow - is masked.




回答3:


set setMasksToBounds to NO .like this

[self.navigationController.view.layer setMasksToBounds:NO];

you can add shadow without loosing corner ,try this with your code

[self.navigationController.view setShouldRasterize:YES];

set some shadowOffset & try

setShadowOffset:CGSizeMake(0, 5)



回答4:


navigationController?.navigationBar.shadowImage = UIImage(named: "shadow")


来源:https://stackoverflow.com/questions/15043038/how-to-add-shadow-to-uinavigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!