Finding free space in UIView
When there is a need to find free space in the UIView to add any subview, we can do this using CAShapeLayer and UIBezierPath. Create a CAShapeLayer for your main view where you will be adding sub views. Let's call it as main shape layer. This will be helpful to check the new view estimated is within the main view. Create a UIBezierPath instance. Whenever a valid new sub view is found, add the edges to this path. Create a random point within the main view. Create a CGRect based on random point as center of your sub view. Let’s call it as estimated view frame. Check the estimated view frame is completely visible in your main view. Else go to step 3. Check your 4 edges of your estimated view frame with path object. If any one of the edge is inside the path, go to step 3. If 4 edges are not inside the path, the estimated view frame is the new view’s frame. Create a new subview and add it to your main view. Add the edges of new view to path. https://github.com/mcabashe...