Pretty standard setup: VC view has top corners rounded via CAShapeLayer with UIBezierPath, like this:
UIBezierPath *cornerPath = [UIBezierPath bezierPathWithRoundedRect:self.view.bounds
byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight
cornerRadii:CGSizeMake(4, 4)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = self.view.bounds;
maskLayer.path = cornerPath.CGPath;
self.view.layer.mask = maskLayer;
While this works, it leads to the whole view being rendered offscreen, surely there should be a way to at least minimize that rendering to corners, or even get rid of it?
Related
I am new in iOS and I am facing problem regarding to curve UIView. I want to curve the radius of the top of UIView like this in the Image
As you can see the screenshot of my work view.
I am using code like this
UIBezierPath *maskPath = [UIBezierPath
bezierPathWithRoundedRect:ViewSwapMyWork.bounds
byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerTopRight)
cornerRadii:CGSizeMake(70, 70)
];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = ViewSwapMyWork.bounds;
maskLayer.path = maskPath.CGPath;
ViewSwapMyWork.layer.mask = maskLayer;
First your view should have the same height and width means it should be square, then you can do like this,
[ViewSwapMyWork.layer setCornerRadius:ViewSwapMyWork.frame.size.width/2];
[ViewSwapMyWork.layer setMasksToBounds:YES];
I am new in iOS. I am make a chat application. The image below is the table message. In each cell, I set 4 corners for messageView and it display correct
self.messageView.layer.cornerRadius = 10.0;
Now, I want some my message only have 3 corners (top-left, bottom-left, bottom-right) and some message have 3 corners (bottom-left, bottom-right, top-right) - like facebook messenger
so I user UIBezierPath for each cell.
But my message don't display full content.
However, when I scroll up/down, the cell recreate and it displays correct.
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
maskLayer.frame = self.bounds;
maskLayer.path = maskPath.CGPath;
self.messageView.layer.mask = maskLayer;
I think my problem is:
When I set the conners by UIBezierPath for my self.messageView, the self.messageView not finished draw yet. so self.messageView.bounds return wrong value.
But I don't know how to fix it.
Any help would be great appreciated
It doesn't seems that you are updating the man in the -layoutSubviews method.
Added layer doesn't know how to deal with your view, so you need to change their size in layoutSubviews of the view that is applying it.
Keep a reference to the mask path and update it, always call super before that.
Try this!!!
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.messageView.bounds byRoundingCorners:(UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerBottomRight) cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
// maskLayer.frame = self.bounds;
maskLayer.frame = self.messageView.bounds; // Try this.
maskLayer.path = maskPath.CGPath;
self.messageView.layer.mask = maskLayer;
I'm using the below code to make rounded corners only for the bottom edges of tableView
CALayer *capa = self.patientTableView.layer;
//Round
CGRect bounds = capa.bounds;
UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:bounds
byRoundingCorners:(UIRectCornerBottomLeft | UIRectCornerBottomRight)
cornerRadii:CGSizeMake(10.0, 10.0)];
CAShapeLayer *maskLayer = [CAShapeLayer layer];
maskLayer.frame = bounds;
maskLayer.path = maskPath.CGPath;
[capa addSublayer:maskLayer];
capa.mask = maskLayer;
This code works fine, but the tableview is not showing up the contents below its frame height (but it scrolls to its offset.y).
You can fix this by putting the table view inside a UIView, and then apply the mask to that container view. That way the mask will not move with the contentOffset of the table view.
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.
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.