2016-03-02 117 views
4

我创造与此类似图像的UIButton: enter image description hereUIButton的阴影图层效果

我尝试了用下面的代码:

+(void)createShadowOnView:(UIView *)view color:(UIColor *)color width:(CGFloat)width height:(CGFloat)height shadowOpacity:(CGFloat)shadowOpacity andShadowRadius:(CGFloat)radius{ 

    view.layer.masksToBounds = NO; 
    view.layer.shadowColor = color.CGColor; 
    view.layer.shadowOffset = CGSizeMake(width,height); 
    view.layer.shadowOpacity = shadowOpacity; 
    [view.layer setShadowRadius:radius]; 
} 

我能做到这一点:

enter image description here

我想让按钮上的阴影效果只保留在底部。

我该如何达到预期的效果。

+0

问题不是在这个方法中,有的地方检查一次 –

+0

你是如何绘制按钮,除了阴影?这是一个图像,还是你用Core Graphics或其他东西绘制它? –

+0

我通过故事板创建它,然后以编程方式创建边框,圆角和阴影。 –

回答

4

只有您的代码问题是您错过了为注册按钮设置背景颜色。使用此代码,

self.signUpButton.backgroundColor = [UIColor whiteColor]; 
[UIView createShadowOnView:self.signUpButton color:[UIColor lightGrayColor] width:0 height:2 shadowOpacity:0.3 andShadowRadius:0]; 
+0

是的,它的工作。奈斯利。谢谢 –

5

也许你应该设置视图的背景颜色,所以标题没有阴影,你可以设置view.layer.shadowOffset来改变阴影尺寸。

UIButton *customBTn = [UIButton buttonWithType:UIButtonTypeCustom]; 
customBTn.backgroundColor = [UIColor whiteColor]; 
customBTn.frame = CGRectMake(100, 100, 200, 50); 
[customBTn setTitle:@"Sign Up" forState:UIControlStateNormal]; 
[customBTn setTitleColor:[UIColor colorWithRed:1/255.0 green:168/255.0 blue:244/255.0 alpha:1.0] forState:UIControlStateNormal]; 
customBTn.layer.borderColor = [UIColor colorWithRed:1/255.0 green:168/255.0 blue:244/255.0 alpha:1.0].CGColor; 
customBTn.layer.borderWidth = 2; 
customBTn.layer.cornerRadius = 25; 
customBTn.layer.shadowColor = [UIColor lightGrayColor].CGColor; 
customBTn.layer.shadowOffset = CGSizeMake(0,8); 
customBTn.layer.shadowOpacity = 0.9; 
[self.view addSubview:customBTn]; 

输出: -

enter image description here

+0

我在故事板创建按钮,并已经尝试过这一点,但事情是,我只是想对整按钮,按钮的底线阴影不。它正在创造整体。 –

+0

昨天我误读了你的请求(只是让标题没有影子),如果你只想让底线有阴影,你可以设置shaowoffset.hope可以帮助你。 –

1

前瞻:

enter image description here

代码:

UIButton *buttonWithShadow = [UIButton buttonWithType:UIButtonTypeCustom]; 
[buttonWithShadow setFrame:CGRectMake(10.0f, 10.0f, 300.0f, 50.0f)]; 
[buttonWithShadow setTitle:@"SIGN UP" forState:UIControlStateNormal]; 
[buttonWithShadow setTitle:@"SIGN UP" forState:UIControlStateHighlighted]; 
[buttonWithShadow.titleLabel setFont:[UIFont boldSystemFontOfSize:15.0f]]; 
[buttonWithShadow setTitleColor:[UIColor colorWithRed:1.0f/255.0 green:168.0f/255.0 blue:244.0f/255.0 alpha:1.0f] forState:UIControlStateNormal]; 
    [buttonWithShadow setBackgroundColor:[UIColor whiteColor]]; 
[buttonWithShadow.layer setBorderColor:[UIColor colorWithRed:1.0f/255.0 green:168.0f/255.0 blue:244.0f/255.0 alpha:1.0f].CGColor]; 
[buttonWithShadow.layer setBorderWidth:2.0f]; 
[buttonWithShadow.layer setCornerRadius:(buttonWithShadow.frame.size.height/2.0f)]; 
[buttonWithShadow.layer setShadowColor:[UIColor lightGrayColor].CGColor]; 
[buttonWithShadow.layer setShadowOpacity:0.3f]; 
[buttonWithShadow.layer setShadowRadius:0.0f]; 
[buttonWithShadow.layer setShadowOffset:CGSizeMake(0.0f,2.0f)]; 
[buttonWithShadow.layer setMasksToBounds:NO]; 
[self.view addSubview:buttonWithShadow]; 
+0

感谢您的回答,但我实际上错过了设置按钮的背景颜色。 –