ios objective-c uibutton shadow

ios - UIButton efecto de capa de sombra



objective-c shadow (3)

Avance:

Código:

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];

Estoy creando un UIButton similar a esta imagen:

Lo intenté usando el siguiente código:

+(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]; }

Pude lograr esto:

Quiero que el efecto de sombra en el botón se mantenga solo en la parte inferior.

¿Cómo puedo lograr el efecto deseado?


El único problema con su código es que perdió para establecer el color de fondo para su botón de registro. Usa este código

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


Tal vez debería establecer el color de fondo de la vista, para que el título no tenga sombra, puede establecer view.layer.shadowOffset para cambiar el tamaño de la sombra.

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];

Salida: -