UIBarButtonItem with rounded corners and shadow

I want to have rounded corners and shadow on a UIBarButtonItem .

 UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

Then I try to set the shadows 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 do a UIBarButtonItem :

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

I do not have rounded corners, but I get a shadow.

If I add:

 useButton.clipsToBounds = YES; 

and / or

 useButton.layer.masksToBounds = YES; 

I get rounded corners, but not shadows. So I thought I'd 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 does not seem to work, I do not see a shadow. I get rounded corners.

Can't have rounded corners & shadows on a UIBarButtonItem / UIButton ?

+8
ios objective-c cocoa-touch uibutton
source share
5 answers

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]]; 
+9
source share
  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]]; 
+5
source share
 btnname.layer.cornerRadius = 8.0; btnname.layer.shadowOffset = CGSizeMake(2, 2); 
+3
source share
 btn.layer.cornerRadius = 2.0 ; 
+1
source share
 buttonName.layer.cornerRadius = 2; buttonName.layer.borderWidth = 1; buttonName.layer.borderColor = [UIColor blacColor].CGColor; 
-2
source share

All Articles