2016-03-02 163 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

感謝您的回答,但我實際上錯過了設置按鈕的背景顏色。 –