This question already has answers here:
How to set cornerRadius for only top-left and top-right corner of a UIView?
(31 answers)
Closed 2 years ago.
I want my UIView round from top right and top left only
view.layer.cornerRadius = 10
Extension of UIView
extension UIView{
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
call function for your view
topView.roundCorners(corners: [.topLeft,.topRight], radius: 8)
Related
I have seen a lot bugs being reported around WKWebView on iOS 13
I was adding rounded corners to WKWebView and UIWebView disappeared on iOS 13 but works fine in iOS 12.
This is how I set the rounded corners on the WKWebView
public override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
webView.roundCorners([.topLeft, .topRight], radius: 10)
}
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds,
byRoundingCorners: corners,
cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
layer.mask = mask
}
}
If I comment out layer.mask = mask, the WKWebView doesn't disappear but also doesn't have rounded corners anymore. Is there a workaround for this?
I want to round top corners of UIView. Emulator iOS version 12.0
My extension
extension UIView
{
func round(corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
How I use it
test.round(corners: [.topRight, .topLeft], radius: 20)
Result
Is it possible to create interaction enabled sidebar solely in the utility area of the storyboard, or does it need any custom UI class?
Related question: How to give shadow like card in iOS
Try this
Create two UIView like this
- Set corner radius to the backView
- Set corner radius to the frontView, only for rightBottom and rightTop portions by using below method
extension UIView {
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
}
Refer my code
https://github.com/leninsmannath/CardViewSample.git
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
I am using this function to give corner radius from particular side for UIButton. I am calling this function into UITableViewCell but I am having issue like I gave constraint to button like its width is changing as per device width change for exa if device is iPhone 5 then its width is 159.5 acoordingly half of screen width. but it is not changing and button is cutting from its width.
I am calling this function in TableviewCell
override func layoutSubviews() {
super.layoutSubviews()
self.btnWithdraw.roundCorners([.bottomRight], radius: 4.0)
self.btnAddFund.roundCorners([.bottomLeft], radius: 4.0)
}
But here issue is like It is taking original frame as per given in storyboard viewcontroller and button is not taking width as per constraints.
Any help would appreciated.
Thank you
Perhaps the problem is that you are assuming that layoutSubviews will be called on the cell under all circumstances where the button is being laid out afresh. That might not be true.
To avoid that assumption, implement your entire masking in the button itself, along these lines:
class MyButton : UIButton {
var whatCorners : UIRectCorner = [.bottomRight]
var whatRadius : CGFloat = 4.0
func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height: radius))
let mask = CAShapeLayer()
mask.path = path.cgPath
self.layer.mask = mask
}
override func layoutSubviews() {
super.layoutSubviews()
self.roundCorners(self.whatCorners, radius: self.whatRadius)
}
}
That way, you can give each button the desired values for whatCorners and whatRadius and the button will round itself.
I currently have three images next to each other. I combined them into a stack view and would like them to have rounded corners like so:
Instead, after setting the corner radius to each image to 5 I have this:
So my question is how I can make the second picture look like the first one? Keep in mind all three stars are in one stack view.
You can pick corners you want to round with this method for UIView:
extension UIView {
func roundCorners(corners: UIRectCorner, radius: CGFloat) {
let mask = CAShapeLayer()
mask.frame = bounds
mask.path = UIBezierPath(roundedRect: bounds, byRoundingCorners: corners, cornerRadii: CGSize(width: radius, height:radius)).cgPath
self.layer.mask = mask
}
}
Usage:
star1.roundCorners(corners: [.topLeft, .bottomLeft], radius: 10)
star3.roundCorners(corners: [.topRight, .bottomRight], radius: 10)
Try out this solution
let path2 = UIBezierPath(roundedRect:view1.bounds, byRoundingCorners:[.TopLeft, .BottomLeft, .TopRight, .BottomRight], cornerRadii: CGSizeMake(20, 20))
let maskLayer2 = CAShapeLayer()
maskLayer2.path = path2.CGPath
view2.layer.mask = maskLayer2
Hope this helps you out.
I put the images behind a view and set the background color to my liking. Then I got rid of any background color for my images. After that, I just rounded the corners of my view.