本文介绍了带有圆角和阴影的 UIBarButtonItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 UIBarButtonItem
上有圆角和阴影.
I want to have rounded corners and shadow on a UIBarButtonItem
.
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
然后我尝试设置阴影和圆角:
Then I try to set shadow and rounded corners:
useButton.layer.masksToBounds = NO;
useButton.layer.cornerRadius = 4;
useButton.layer.shadowOffset = CGSizeMake(0, 1.5);
useButton.layer.shadowRadius = 0.5;
useButton.layer.shadowOpacity = 1.0;
useButton.layer.shadowColor = [BSTCMColorUtility colorFromHexString:@"#AFAFAF"].CGColor;
最后我制作了 UIBarButtonItem
:
UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
[self.navigationItem setRightBarButtonItems:@[useItem]];
没有圆角,但有阴影.
如果我添加:
useButton.clipsToBounds = YES;
和/或:
useButton.layer.masksToBounds = YES;
我得到圆角,但没有阴影.所以我想我会尝试添加一个子层.
I get rounded corners, but no shadow. So I thought that I would try to add a sublayer.
CALayer *useButtonShadowLayer = [CALayer new];
useButtonShadowLayer.frame = useButton.frame;
useButtonShadowLayer.cornerRadius = 4;
useButtonShadowLayer.backgroundColor = [UIColor whiteColor].CGColor;
useButtonShadowLayer.shadowOffset = CGSizeMake(0, 1.5);
useButtonShadowLayer.shadowRadius = 0.5;
useButtonShadowLayer.shadowOpacity = 1.0;
useButtonShadowLayer.shadowColor = [BSTCMColorUtility colorFromHexString:@"#AFAFAF"].CGColor;
useButton.layer.cornerRadius = 4;
useButton.layer.masksToBounds = YES;
UIView *parent = useButton.superview;
[parent.layer insertSublayer:useButtonShadowLayer below:useButton.layer];
它似乎不起作用,我看不到阴影.我得到了圆角.
It doesn't seem to work, I see no shadow. I get rounded corners tho.
UIBarButtonItem
/UIButton
上不可能有圆角和阴影吗?
Isn't possible to have rounded corners AND shadow on a UIBarButtonItem
/UIButton
?
推荐答案
试试这个更新的代码:
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
useButton.frame = CGRectMake(100, 430, 100, 40);
useButton.layer.masksToBounds = NO;
useButton.layer.cornerRadius = 10;
useButton.layer.shadowOffset = CGSizeMake(1.5, 1.5);
useButton.layer.shadowRadius = 0.5;
useButton.layer.shadowOpacity = 1.0;
useButton.layer.shadowColor = [UIColor blackColor].CGColor;
useButton.backgroundColor = [UIColor redColor];
UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
[self.navigationItem setRightBarButtonItems:@[useItem]];
这篇关于带有圆角和阴影的 UIBarButtonItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!