Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift 3: nested UIView(s) corners not getting rounded

I have a UIView named parent. Inside parent are nested two UIViews named Child1 and Child2.(see image) Parent-Child Relation

Note: There are no margins i.e. 0 margin b/w both Child1, Child2 and Parent.

I am trying to round the corners of Parent.

parentView.layer.cornerRadius = 10

This does not round up the corners of the children.(see image)

The top corners do not get rounded

The top corners do not end up getting round here. The bottom corners are rounded because Child2 is transparent while Child1 is colored. Parent is white in color.

What I've tried so far:

parentView.layer.cornerRadius = 10
child1.clipsToBounds = true

No luck

parentView.layer.cornerRadius = 10
let maskLayer = CAShapeLayer()
maskLayer.path = UIBezierPath(roundedRect: view.bounds, byRoundingCorners: [.topLeft, .topRight], cornerRadii: CGSize(width: 10, height: 10)).cgPath
child1.layer.mask = maskLayer

Still No luck

Please help me out

like image 477
Aryan Sharma Avatar asked Jul 29 '17 11:07

Aryan Sharma


People also ask

How do you round UIView corners?

If you start with a regular UIView it has square corners. You can give it round corners by changing the cornerRadius property of the view's layer . and smaller values give less rounded corners. Both clipsToBounds and masksToBounds are equivalent.

How do I change the radius of a corner button in Xcode?

There is no direct way to set corner radius for your button in xcode attribute inspector. So what you should do is, create a user defined run time attribute for corner radius. Key Path : layer. cornerRadius Type : Number Value : 8 //or whatever value you want for your corner radius.


2 Answers

I think you need to add clipToBound on parentView

parentView.clipsToBounds = true
like image 182
Vivek Avatar answered Sep 28 '22 12:09

Vivek


If you need cornerRadius

import QuartzCore

parentView.layer.cornerRadius = yourvalue

if you don't want to shadow then add

parentView.layer.masksToBounds = true 
like image 26
Raksha Saini Avatar answered Sep 28 '22 11:09

Raksha Saini