I have a rounded UIImageView. When I add a shadow to it, I lose the rounded corner. How can I have a shadow with a rounded corner?
//Avatar
CGRect rect;
rect = CGRectMake(13, 10, 48, 48);
avatarImageView = [[TCImageView alloc] initWithURL:[NSURL URLWithString:nil] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
avatarImageView.frame = rect;
avatarImageView.caching = YES;
//Round the corners
CALayer * layer = [avatarImageView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:9.0];
//Add a shadow
avatarImageView.layer.shadowColor = [UIColor grayColor].CGColor;
avatarImageView.layer.shadowOffset = CGSizeMake(0, 1);
avatarImageView.layer.shadowOpacity = 1;
avatarImageView.layer.shadowRadius = 9.0;
avatarImageView.clipsToBounds = NO;
[self.contentView addSubview: avatarImageView];
Make UIImageView Corners RoundedSetting the corner radius to 100 will make the image view completely rounded. Try different corner radius like 10, 20, 30, 40 to get image corners of different radius. To make the image border and visible I will set the borderWidth and the borderColor.
For changing the shadow color you use the shadowColor property of the material. Which takes the Color object as an input. For adding a drop shadow to flutter TextField or TextFormField you can use the Material widget. Just wrap TextField or TextFormField with Material widget and set elevation and shadow color.
The feature, which makes your image appear with rounded corner is the one which hides the shadow: [layer setMasksToBounds: YES]
. The thing you can do, put your ImageView within a UIView subview which acts as a container providing the shadow.
So code could look like this (I just typed it down, did not compile it though)
//Avatar
CGRect rect;
rect = CGRectMake(13, 10, 48, 48);
avatarImageView = [[TCImageView alloc] initWithURL:[NSURL URLWithString:nil] placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
avatarImageView.frame = rect;
avatarImageView.caching = YES;
//Round the corners
CALayer * layer = [avatarImageView layer];
[layer setMasksToBounds:YES];
[layer setCornerRadius:9.0];
//Add a shadow by wrapping the avatar into a container
UIView * container = [[UIView alloc] initWithFrame: rect];
avatarImageView.frame = CGRectMake(0,0,rect.size.width, rect.size.height);
// setup shadow layer and corner
container.layer.shadowColor = [UIColor grayColor].CGColor;
container.layer.shadowOffset = CGSizeMake(0, 1);
container.layer.shadowOpacity = 1;
container.layer.shadowRadius = 9.0;
container.layer.cornerRadius = 9.0;
container.clipsToBounds = NO;
// combine the views
[container addSubview: avatarImageView];
[self.contentView addSubview: container];
[container release];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With