简体   繁体   English

带圆角和阴影的UIBarButtonItem

[英]UIBarButtonItem with rounded corners and shadow

I want to have rounded corners and shadow on a UIBarButtonItem . 我想在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;

Finally I make the UIBarButtonItem : 最后我创建了UIBarButtonItem

UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
[self.navigationItem setRightBarButtonItems:@[useItem]];

I get no rounded corners, but I do get the shadow. 我没有圆角,但我确实得到了阴影。

If I add: 如果我添加:

useButton.clipsToBounds = YES;

And/or: 和/或:

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. 我得到了圆角。

Isn't possible to have rounded corners AND shadow on a UIBarButtonItem / UIButton ? UIBarButtonItem / UIButton上不可能有圆角和阴影?

Try this Updated code : 试试这个更新的代码:

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]];
    UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
    useButton.frame = CGRectMake(0, 0, 60, 30);
    useButton.backgroundColor = [UIColor redColor];
    useButton.layer.masksToBounds = NO;
    useButton.layer.cornerRadius = 4;
    useButton.layer.shadowOffset = CGSizeMake(0, 1.5);
    useButton.layer.shadowRadius = 5;
    useButton.layer.shadowOpacity = 1.0;
//    useButton.layer.shadowColor = [UIColor blackColor].CGColor;
    useButton.layer.borderColor = [UIColor blackColor].CGColor;
    [self.view addSubview:useButton];

    UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
    [self.navigationItem setRightBarButtonItems:@[useItem]];
btnname.layer.cornerRadius = 8.0;
btnname.layer.shadowOffset = CGSizeMake(2, 2);
btn.layer.cornerRadius = 2.0 ;
buttonName.layer.cornerRadius = 2;
buttonName.layer.borderWidth = 1;
buttonName.layer.borderColor = [UIColor blacColor].CGColor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM