Button rounded corners only .TopRight and .BottomRight with constrains - ios

Helo guys i found How to create rounded UIButton with top-left & bottom-left corner radius only and its working. But problem is when button have some constrains it suddenly stop workin. Can anyone help me with that i tried change everything and still no results.
As you can see on picture its working for first orange Button in top of a picture but for TWITTER button its not working. Any help please ?
extension UIButton{
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
}
}
and this is usage ( testBtn is that orange working button)
testBtn.roundCorners([.TopRight, .BottomRight], radius: 20)
facebookBtn.roundCorners([.TopLeft, .BottomLeft], radius: 20)
twitterBtn.roundCorners([.TopRight, .BottomRight], radius: 20)

Related

Need to make particular corner round of view [duplicate]

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)

iOS UIView rounding top corners works just for top left

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

Bezierpath button width issue

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.

How to round corners on specific sides of UIImages?

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.

Round top corners of UIImageView like a UIButton

Here is my problem: I am trying to round the top corners of an UIImageView to look like a UIButton with rounded corner. I am using Masks to do that but the result I have isn't what I am really expecting...
I am using the following extension:
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
}
}
Calling it like this in my code:
imageView.roundCorners([.TopLeft, .TopRight], radius: 80)
And here is the result:
Rounded corners error
I would like to have the top corner to look like the bottom corners (bottom corners are UIButton corners of radius 10) but I don't see where the error is...
Thanks you all for your help !
EDIT: I was using the right code, I just didn't notice that my UIImageView was larger than the UIImage hence the weird corners... I create the UIImageView programmatically and thus didn't notice the size difference... Newbie's mistake...
Thanks you all for you help !
Use this value:
imageView([.TopLeft, .TopRight], radius: 20)

Resources