Adding Rounded Corners to only top of UITableView? - ios

I am trying to add rounded corners to only the top corners of my UITableView but the problem is, with the code below, it just makes a black layer over my whole UITableView. How would I fix this?
//Rounded Corners for top corners of UITableView
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:thetableView.bounds
byRoundingCorners:UIRectCornerTopLeft
cornerRadii:CGSizeMake(10.0, 10.0)];
UIBezierPath *maskPath2 = [UIBezierPath bezierPathWithRoundedRect:thetableView.bounds
byRoundingCorners:UIRectCornerTopRight
cornerRadii:CGSizeMake(10.0, 10.0)];
// Create the shape layer and set its path
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = thetableView.bounds;
maskLayer.path = maskPath.CGPath;
CAShapeLayer *maskLayer2 = [CAShapeLayer layer];
maskLayer.frame = thetableView.bounds;
maskLayer.path = maskPath2.CGPath;
// Set the newly created shape layer as the mask for the image view's layer
[thetableView.layer addSublayer:maskLayer];
[thetableView.layer addSublayer:maskLayer2];

the following code works for me for a navigationbar, just change the first line and put your self.tableView:
CALayer *capa = [self.navigationController navigationBar].layer;
[capa setShadowColor: [[UIColor blackColor] CGColor]];
[capa setShadowOpacity:0.85f];
[capa setShadowOffset: CGSizeMake(0.0f, 1.5f)];
[capa setShadowRadius:2.0f];
[capa setShouldRasterize:YES];
//Round
CGRect bounds = capa.bounds;
bounds.size.height += 10.0f; //I'm reserving enough room for the shadow
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;

I think the best way to make a background view for the table view which view has rounded top corner.Its just a solution to solve the problem.May be it will help you.

Related

how to set cornerRadius for RoundedRectagle only bottom-left,bottom-right of a UIButton?

I tried to set button bottom rounded rectangle. But they apply only Right of bottom.
I want bothside bottom of button has rounded rectangle.
This is my code:
UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:registerbtn.bounds
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(9.0, 9.0)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = registerbtn.bounds;
shapeLayer.path = shapePath.CGPath;
[registerbtn.layer addSublayer:shapeLayer];
Output:
This is working fine. just try it
UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:registerbtn.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(9.0, 9.0)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = registerbtn.bounds;
shapeLayer.path = shapePath.CGPath;
[registerbtn.layer addSublayer:shapeLayer];
Your code looks fine.
Just write same code in viewWillAppear and check:
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
UIBezierPath *shapePath = [UIBezierPath bezierPathWithRoundedRect:registerbtn.bounds
byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight
cornerRadii:CGSizeMake(9.0, 9.0)];
CAShapeLayer *shapeLayer = [CAShapeLayer layer];
shapeLayer.frame = registerbtn.bounds;
shapeLayer.path = shapePath.CGPath;
[registerbtn.layer addSublayer:shapeLayer];
}
Above mentioned answers should work but the problem is your button is getting rounded from both sides but its effect is not visible to you. If you will check by zooming then you can see your button is cornered and then there is another layer under the above layer on which you have added bezierpath.
let shapePath = UIBezierPath.init(roundedRect: view.bounds, byRoundingCorners: [UIRectCorner.BottomLeft,UIRectCorner.BottomRight], cornerRadii: CGSizeMake(8,8))
let newCornerLayer = CAShapeLayer()
newCornerLayer.frame = view.bounds
newCornerLayer.path = shapePath.CGPath;
view.layer.mask = newCornerLayer
This should work but you need to be careful on which you are applying the layer.

Unable to apply UIBezierPath in CellForRowAtIndexPath before scroling the TableView

Using This code I got My Profile Picture in Proper D shape but after When i scrolling the tableView not when we are entering in to view controller.
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:cellimg.boundsbyRoundingCorners:(UIRectCornerBottomRight|UIRectCornerTopRight)cornerRadii:CGSizeMake(40.0, 40.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame =cellimg.bounds;
maskLayer.path = maskPath.CGPath;
cellimg.layer.mask = maskLayer;
Use D shape mask image with below code it may be work for you
CALayer *mask = [CALayer layer];
mask.contents = (id)[UIImage imageNamed:#“your_D_ShapeMask.png"].CGImage;
mask.frame = CGRectMake(0, 0, 100, 100);
[cellimg.layer setMask:mask];

UITextField did not round the border

I used this to round top two corners of my UITextFeild
CAShapeLayer * maskLayer = [CAShapeLayer layer];
maskLayer.path = [UIBezierPath bezierPathWithRoundedRect: self.emailFeild.bounds byRoundingCorners: UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii: (CGSize){10.0, 10.}].CGPath;
I also have border added to this UITextField as
self.emailFeild.layer.borderColor = [[self colorFromHexString:#"0x3263A3"]CGColor];
self.emailFeild.layer.borderWidth = 1.0f;
self.emailFeild.layer.mask = maskLayer;
but when I run it. It did not round the corners of border and I get this result
The mask layer doesn't get drawn, just used to compute the mask. Try:
-(void)roundCorners:(UIRectCorner)corners radius:(CGFloat)radius
{
CGRect bounds = self.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:corners
cornerRadii:CGSizeMake(radius, radius)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
self.layer.mask = maskLayer;
CAShapeLayer* frameLayer = [CAShapeLayer layer];
frameLayer.frame = bounds;
frameLayer.path = maskPath.CGPath;
frameLayer.strokeColor = [UIColor redColor].CGColor;
frameLayer.fillColor = nil;
[self.layer addSublayer:frameLayer];
}
-(void)roundTopCornersRadius:(CGFloat)radius
{
[self roundCorners:(UIRectCornerTopLeft|UIRectCornerTopRight) radius:radius];
}

CAShapeLayer resulting in no image in UIImageView

I'm trying to make uiimageview top corners rounded but when CASharpeLayer is drawn image gets disappear, i don't know what causing this issue i've put below code in willDisplayCell. when cell is reused images appears properly with drawn layer
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:cell.imgProduct.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(3.0,3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = cell.imgProduct.bounds;
maskLayer.path = maskPath.CGPath;
cell.imgProduct.layer.mask = maskLayer;
cell.imgProduct.layer.masksToBounds=NO;
I've solved this by myself...:P i've drawn the mask in drawRect method of my UICollectionViewCell subclass
- (void)drawRect:(CGRect)rect {
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.imgProduct.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(3.0, 3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.imgProduct.bounds;
maskLayer.path = maskPath.CGPath;
_imgProduct.layer.mask = maskLayer;
_imgProduct.layer.masksToBounds=YES;
}

Curve-inside UIView top border

I want to curve inside the top border of UIView like this,
I've tried many ways like UIBezierPath etc.
Help needed.
You can do that using UIBezierPath, Have a look at this answer,
UIBezierPath *maskPath;
maskPath = [UIBezierPath bezierPathWithRoundedRect:_backgroundImageView.bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(3.0, 3.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
_backgroundImageView.layer.mask = maskLayer;
[maskLayer release];
With this we can draw the rounded corners to any side of view.
But If you want to draw rounded edge, Better to take a background image to view.

Resources