Ios 带圆角和阴影的uibarbuttonite
我想在Ios 带圆角和阴影的uibarbuttonite,ios,objective-c,cocoa-touch,uibutton,Ios,Objective C,Cocoa Touch,Uibutton,我想在uibarbuttonite上有圆角和阴影 UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom]; 然后我尝试设置阴影和圆角: useButton.layer.masksToBounds = NO; useButton.layer.cornerRadius = 4; useButton.layer.shadowOffset = CGSizeMake(0, 1.5); useButton.layer.shado
uibarbuttonite
上有圆角和阴影
UIButton *useButton = [UIButton buttonWithType:UIButtonTypeCustom];
然后我尝试设置阴影和圆角:
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;
最后,我制作了uibarbuttoneim
:
UIBarButtonItem *useItem = [[UIBarButtonItem alloc] initWithCustomView:useButton];
[self.navigationItem setRightBarButtonItems:@[useItem]];
我没有圆角,但我有阴影
如果我加上:
useButton.clipsToBounds = YES;
和/或:
useButton.layer.masksToBounds = YES;
我有圆角,但没有阴影。所以我想我会尝试添加一个子层
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];
它似乎不起作用,我看不到影子。我有圆角
在
UIBarButtonItem上不可能有圆角和阴影
/UIButton
?尝试此更新的代码:
buttonName.layer.cornerRadius = 2;
buttonName.layer.borderWidth = 1;
buttonName.layer.borderColor = [UIColor blacColor].CGColor;
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]];
自定义类型。这是在问题中,第一行代码在我的问题的第一个版本中,我忘了提到我使用按钮来制作一个uibarbuttonite。也许这就是问题所在……它是有效的,但当我用按钮制作一个uibarbuttonite时就不行了。我忘了在我的问题的第一个版本中提到这一点。谢谢,我不确定我的错误在哪里,我将您的所有代码粘贴到一个新项目中,它在那里工作。所以我在我的项目中的其他地方有一个错误:)我不知道这是如何回答这个问题的。如果将
masksToBounds
设置为NO
,则角半径不再可见。谢谢。这一定是因为我实际上是在UIBarButtonItem中“包装”UIButton,以便在导航栏中设置右栏按钮项。
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 ;