我想要圆角和阴影UIBarButtonItem.
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
Run Code Online (Sandbox Code Playgroud)
然后我尝试设置阴影和圆角:
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;
Run Code Online (Sandbox Code Playgroud)
最后我做了UIBarButtonItem:
UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
[self.navigationItem setRightBarButtonItems:@[useItem]];
Run Code Online (Sandbox Code Playgroud)
我没有圆角,但我确实得到了阴影.
如果我添加:
useButton.clipsToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
和/或:
useButton.layer.masksToBounds = YES;
Run Code Online (Sandbox Code Playgroud)
我得到圆角,但没有阴影.所以我想我会尝试添加一个子图层.
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 …Run Code Online (Sandbox Code Playgroud)