I want to apply two types of border on a UIImageView
:
layer
of the UIImageView
.layer
of the UIImageView
.How can I do this?
Adding border to ImageView image makes your ImageView highlight from the activity layout surface so image will look individually on application. We can easily set border on ImageView using external layout.xml file setting as ImageView background.
Add stroke on ImageView image to display image with border using xml file. Adding border to ImageView image makes your ImageView highlight from the activity layout surface so image will look individually on application. We can easily set border on ImageView using external layout.xml file setting as ImageView background.
Adding a border to a UIView can be implemented by setting the borderColor and borderWidth properties on the view’s layer: An @IBDesignable UIView extension marking the border properties as @IBInspectable can be implemented to allow view borders to be configured in a Storyboard:
Shadows are drawn outside of a view’s bounds, meaning clipToBounds and the view’s layer masksToBounds properties must be false for the shadow to be visible. A UIImageView with contentMode set to .aspectFill, .center, and others will draw the image outside of the UIImageView bounds if clipToBounds is false.
Try
#define kBorderWidth 3.0
#define kCornerRadius 8.0
CALayer *borderLayer = [CALayer layer];
CGRect borderFrame = CGRectMake(0, 0, (imageView.frame.size.width), (imageView.frame.size.height));
[borderLayer setBackgroundColor:[[UIColor clearColor] CGColor]];
[borderLayer setFrame:borderFrame];
[borderLayer setCornerRadius:kCornerRadius];
[borderLayer setBorderWidth:kBorderWidth];
[borderLayer setBorderColor:[[UIColor redColor] CGColor]];
[imageView.layer addSublayer:borderLayer];
And don't forget to import QuartzCore/QuartzCore.h
This example will draw a boarder on the layer, but change it's frame slightly to make the border around the layer.
Another way
You must import
#import <QuartzCore/QuartzCore.h>
Then add code for your UIImageView
imgView.clipsToBounds = YES;
imgView.layer.cornerRadius = 8.0;
imgView.layer.borderWidth = 2.0;
imgView.layer.borderColor = [UIColor greenColor].CGColor;
An other way is to add another layer that goes a bit outside of the UIImageView's layer like so :
CALayer * externalBorder = [CALayer layer];
externalBorder.frame = CGRectMake(-1, -1, myView.frame.size.width+2, myView.frame.size.height+2);
externalBorder.borderColor = [UIColor blackColor].CGColor;
externalBorder.borderWidth = 1.0;
[myView.layer addSublayer:externalBorder];
myView.layer.masksToBounds = NO;
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