cornerRadius showing background colour in corners - ios

To achieve the above design I use the below code. Every thing is perfect.
for(....)
{
CALayer *layer=[CALayer layer];
[layer setFrame:CGRectMake(xAxis, 2.0f, width, height)];
[layer setBackgroundColor:[self getColor:colorId]];
[[self layer] addSublayer:layer];
}
I have use cornerRadious for rounding the corner. And the problem is on corners it's shows the background colours little bit. Can anybody please suggest me what to do. I am using below code for cornerRadious. Thanks in advance.
[[vBarHolder layer] setCornerRadius:3.0f];
[[vBarHolder layer] setBorderColor:[[UIColor whiteColor] CGColor]];
[[vBarHolder layer] setBorderWidth:2.0f];
[[vBarHolder layer] setMasksToBounds:YES];
[vBarHolder setClipsToBounds:YES];

Your coloured views are overlapped by vBarHolder.
Modify the code as below.
for(....)
{
CALayer *layer=[CALayer layer];
[layer setFrame:CGRectMake(xAxis + 2, 2.0f + 2, width - 4 , height- 4)];
[layer setBackgroundColor:[self getColor:colorId]];
[[self layer] addSublayer:layer];
}
as the layers are having corner radius as 0 you are able to see in background.
Regards,
Amit

Related

Add Rounded corners after resizing UIImageView

Facing problem when adding rounded corners to imageview.
here is my code
CALayer * lay = [imageview layer];
[lay setMasksToBounds:YES];
[lay setCornerRadius:10.0];
[lay setBorderWidth:5.0];
[lay setBorderColor:[[UIColor clearColor] CGColor]];
imageview.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageview.layer.shouldRasterize = YES;
And its working fine but if I am resizing the imageview, its not making corners round.
Here is my resizing code:
[UIView animateWithDuration:0.2 animations:^ {
[imageview setTransform:CGAffineTransformMakeScale(1.1, 1.1)];
CALayer * l = [imageview layer];
[l setMasksToBounds:YES];
[l setCornerRadius:10.0];
[l setBorderWidth:5.0];
[l setBorderColor:[[UIColor clearColor] CGColor]];
imageview.layer.rasterizationScale = [UIScreen mainScreen].scale;
imageview.layer.shouldRasterize = YES;
newimage = imageview.image;
[[NSUserDefaults standardUserDefaults] setValue:UIImageJPEGRepresentation(newimage,1.1) forKey:#"newimage"];
}];
Any help would be appreciated.
You can set corner radius from storyboard or XIB hope it will work, Please check This.
Plz import the QuartzCore/QuartzCore.h
and then write the code ...
yourImageView.layer.cornerRadius = yourRadius;
yourImageView.clipsToBounds = YES;
Why to write the clipsToBounds property
A Boolean value that determines whether subviews are confined to the bounds of the view.
Discussion
Setting this value to YES causes subviews to be clipped to the bounds of the receiver. If set to NO, subviews whose frames extend beyond the visible bounds of the receiver are not clipped. The default value is NO.
Try
[UIView animateWithDuration:0.2
delay:0.1
options: UIViewAnimationOptionCurveEaseOut
animations:^
{
[imageview setTransform:CGAffineTransformMakeScale(1.1, 1.1)];
}
completion:^(BOOL finished)
{
[imageview.layer setCornerRadius:10.0];
imageview.clipsToBounds = YES;
imageview.layer.masksToBounds = YES;
[imageview.layer setBorderWidth:5.0];
[imageview.layer setBorderColor:[[UIColor clearColor] CGColor]];
newimage = imageview.image;
[[NSUserDefaults standardUserDefaults] setValue:UIImageJPEGRepresentation(newimage,1.1) forKey:#"newimage"];
}];
You have to round corners after animation complete. You may get animation completion as above.

How to add left and right margin for the table cell section?

I'm new to iOS, I have an grouped table view, one section one row as below. How can I set the left and right margin for the cell?
Here is the code I set for the border...
[cell.layer setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[cell.layer setBorderWidth:1.0f];
[cell.layer setCornerRadius:5];
Expected result:
The best programming practise for this is subclassing your UITableViewCell and override its setFrame method.
- (void)setFrame:(CGRect)frame {
frame.origin.x += 10;
frame.size.width -= 20;
[super setFrame:frame];
}
Also, you can set the corner radius and colour of the cell in drawRect method
- (void)drawRect:(CGRect)rect {
[super drawRect:rect];
// border radius
[self.layer setCornerRadius:5.0f];
// border
[self.layer setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[self.layer setBorderWidth:1.0f];
}
One more thing, if you just want to make the cell a little more attractive, add this method too in the drawRect:
[self.layer setShadowColor:[UIColor lightGrayColor].CGColor];
[self.layer setShadowOpacity:0.8];
[self.layer setShadowRadius:3.0];
[self.layer setShadowOffset:CGSizeMake(2.0, 2.0)];
do like
Step -1
create the one Common UIView named as BackgroundView .and set subview to cell.contentView
Step -2
set the frame on BackgroundView whatever you need. based on contentview width
Step -3
add the Label, Date, Image to subview of BackgroundView.
Step -4
then set the layer for BackgroundView, final answer you will get
[cell.BackgroundView setBorderColor:[UIColor colorWithRed:0.00 green:0.60 blue:1.00 alpha:1.0].CGColor];
[cell.BackgroundView setBorderWidth:1.0f];
[cell.BackgroundView setCornerRadius:5];

Draw a gesture control resizable circle in iOS

I want to draw a circle in iOS. It can be an image view or view, I want to adjust its size on gestures. Like, dragging from outside to the center, the radius of the circle should reduce and so vice versa.
Please note that I want to fix the position of that view, circle or image. I just want to adjust the size as it is dragged.
try this
- (IBAction)pinch:(UIPinchGestureRecognizer *)sender {
double scale=[sender scale];
self.view1.frame=CGRectMake(self.view1.frame.origin.x*scale, self.view1.frame.origin.y*scale, self.view1.frame.size.width*scale, self.view1.frame.size.height*scale);
[self.view1.layer setCornerRadius:5];
}
Try this.
- (IBAction)pan:(UIPanGestureRecognizer *)sender {
for (CALayer *layer in self.imageView.layer.sublayers) {
[layer removeFromSuperlayer];
}
CGPoint scale =[sender locationInView:sender.view];
CAShapeLayer *circleLayer = [CAShapeLayer layer];
[circleLayer setBounds:CGRectMake(0.0f, 0.0f, [_imageView bounds].size.width,
[_imageView bounds].size.height)];
[circleLayer setPosition:CGPointMake(CGRectGetMidX([_imageView bounds]),CGRectGetMidY([_imageView bounds]))];
UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, CGRectGetWidth(_imageView.frame)+scale.x, CGRectGetHeight(_imageView.frame)+scale.y)];
[circleLayer setPath:[path CGPath]];
[circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
[circleLayer setLineWidth:1.0f];
[circleLayer setFillColor:[[UIColor clearColor] CGColor]];
[[_imageView layer] addSublayer:circleLayer];
}

IOS: round the cournes of a uicollectionViewCell clipin it's contents

I have a collection view with multiple cell styles and sizes I want to round the corners of the cell using this helper function:
void roundCornersOfAView(UIView *view, CGFloat radius, BOOL shadow){
CALayer* layer = [view layer];
[layer setCornerRadius:radius];
[layer setBorderColor:[UIColor blackColor].CGColor];
[layer setMasksToBounds:YES];
// CALayer* mask = [CALayer layer];
// [mask setFrame:CGRectMake(layer.frame.origin.x, layer.frame.origin.y, layer.frame.size.width, layer.frame.size.height)];
// [mask setCornerRadius:radius];
// [layer setMask:mask];
if (shadow) {
[layer setBorderWidth:0.5f];
//[layer setMasksToBounds:NO];
[layer setShadowColor:[UIColor blackColor].CGColor];
[layer setShadowOpacity:1.0];
[layer setShadowRadius:1.0];
[layer setShadowOffset:CGSizeMake(0.0, 0.0)];
}
[layer setMasksToBounds:YES];
}
using it like this in my cell for row at index path:
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:... forIndexPath:indexPath];
roundCornersOfAView([cell contentView], 30, YES);
The problem is that images on cell are not clipped in the rounded courners, neither labels or any other subviews, it only clips the background color... what is the best way to round courners and clip its content just like the instagram app?
Calling it like this:
roundCornersOfAView(cell, 30, YES);
using the helper function like this:
void roundCornersOfAView(UIView *view, CGFloat radius, BOOL shadow){
CALayer* layer = [view layer];
[layer setCornerRadius:radius];
[layer setBorderWidth:0.0f];
if (shadow) {
[layer setBorderColor:[UIColor blackColor].CGColor];
[layer setBorderWidth:0.1f];
[layer setShadowColor:[UIColor blackColor].CGColor];
[layer setShadowOpacity:0.5];
[layer setShadowRadius:1.5];
[layer setShadowOffset:CGSizeMake(0.0, 1.0)];
}
[layer setMasksToBounds:YES];
}
The problem was that I was sending the content view instead of the cell itself.

Corner Radius leaving circular line

After setting border width and corner radius of UIView getting black circular line
Code:
afterSelectFrameView.layer.borderWidth=[slide value];
afterSelectFrameView.layer.cornerRadius=[slide value];
Apply mask to bounds
[imageView.layer setMasksToBounds:YES];
[imageView.layer setCornerRadius:5.0];
setting the border is not necessary, but here is an option for you.
[imageView.layer setBorderWidth:0.0f];
I use the code below (it is part of a ViewUtilities library) to round the corners on any view.
You could modify it to add parameters for other radii, colors, etc.
+(void)AdjustViewEdges:(UIView*)view
{
[view.layer setBorderColor: [[UIColor blackColor] CGColor]];
[view.layer setBorderWidth: 1.0];
[view.layer setCornerRadius:12.0f];
[view.layer setMasksToBounds:YES];
view.backgroundColor = [UIColor colorWithRed:.99 green:0.99 blue:0.99 alpha:1.0];
}
// You will need this as well
#import <QuartzCore/QuartzCore.h>
I don't believe it is giving me the lines you are seeing...but my background colors were different so they may be hidden...
Was this helpful?
Use it like this
afterSelectFrameView.layer.borderWidth=0.0;
afterSelectFrameView.layer.cornerRadius=[slide value];
Happy coding..........

Resources