Set corner radius to particular side of UIView/UIButton only

无人久伴 提交于 2019-12-11 01:56:57

问题


I want to set corner radius to my UI element as shown in attached snap:

I tried setting corner radius using UIBezierPath but it doesn't give expected result. I also added those three UIButtons in UIView and set corner radius to Container view but no use.

With reference to https://stackoverflow.com/a/31233171/4886069 , Following is the code what I've tried on top and bottom buttons:

let rectShape = CAShapeLayer()
rectShape.bounds = self.scanButton.frame
rectShape.position = self.scanButton.center
rectShape.path = UIBezierPath(roundedRect: self.scanButton.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.scanButton.layer.mask = rectShape

let rectShape1 = CAShapeLayer()
rectShape1.bounds = self.manualButton.frame
rectShape1.position = self.manualButton.center
rectShape.path = UIBezierPath(roundedRect: self.manualButton.bounds, byRoundingCorners: [.bottomLeft, .bottomRight], cornerRadii: CGSize(width: 20, height: 20)).cgPath
self.manualButton.layer.mask = rectShape1

Following is the code what I've tried on container view:

containerView.layer.cornerRadius = 20

following is the output what I got:

Any help appreciated. Thank you


回答1:


Use topRight, bottomLeft, and bottomRight in byRoundingCorners: [.topLeft] option for other corner for your expected result.




回答2:


This code is for swift 2.0 just convert this code into swift 3.0

cancelbnutton.layer.cornerRadius = 10.0
 cancelbnutton.clipsToBounds = true
 let rectShape = CAShapeLayer()
 rectShape.bounds = scanbutton.frame
 rectShape.position = scanbutton.center

 rectShape.path = UIBezierPath(roundedRect: scanbutton.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.TopRight], cornerRadii: CGSizeMake(10, 10)).CGPath
 scanbutton.layer.mask = rectShape

 let rectShape1 = CAShapeLayer()
 rectShape1.bounds = manualbutton.frame
 rectShape1.position = manualbutton.center

 rectShape1.path = UIBezierPath(roundedRect: manualbutton.bounds, byRoundingCorners: [UIRectCorner.BottomLeft , UIRectCorner.BottomRight], cornerRadii: CGSizeMake(10, 10)).CGPath
 manualbutton.layer.mask = rectShape1




回答3:


Use this for giving Corner radius

let Path = UIBezierPath(roundedRect: button.bounds, byRoundingCorners: [UIRectCorner.TopLeft , UIRectCorner.BottomLeft], cornerRadii: CGSizeMake(1.0, 1.0)).CGPath


来源:https://stackoverflow.com/questions/40169745/set-corner-radius-to-particular-side-of-uiview-uibutton-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!