0

enter image description here

as you can see the top of my circle is protruding out the bottom of the screen. My code is pretty normal and I've tried numerous ways to fix this such as by making the child view in storyboard and setting the centre to the parent view but nothing is working.


class ModalViewController: UIViewController {
    
    override func viewDidLoad() {
        
        super.viewDidLoad()

        let Panel = UIView()
        Panel.frame = CGRect(x: 0, y: 400, width: 400, height: 200)
        Panel.backgroundColor = UIColor.white
        
        view.backgroundColor = UIColor.clear
        modalPresentationStyle = .overCurrentContext
        view.addSubview(Panel)
        
        let shapeLayer = CAShapeLayer()
        let CircularPath = UIBezierPath(ovalIn: Panel.frame)
        shapeLayer.path = CircularPath.cgPath
        Panel.layer.addSublayer(shapeLayer)
    }```
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • the wrong line is `let CircularPath = UIBezierPath(ovalIn: Panel.frame)` you should change it to `let CircularPath = UIBezierPath(ovalIn: Panel.bounds)`. Since the shapeLayer is sublayer of Panel, in Panel's coordinate, the origin of shapeLayer should be (0;0) instead of frame's origin (0, 400) – nghiahoang Aug 05 '20 at 08:43
  • one more thing, you should follow the Swift's coding convention, the variable name should be `camelCase`, `Panel` should be `panel`, `CircularPath` should be `circularPath` – nghiahoang Aug 05 '20 at 08:44

1 Answers1

0

The problem is in

let CircularPath = UIBezierPath(ovalIn: Panel.frame)

Since you are trying to add a circle that covering the Panel view, it should be

let circularPath = UIBezierPath(ovalIn: panel.bounds)

See more about frame vs bounds here

Johnykutty
  • 12,091
  • 13
  • 59
  • 100
  • that worked thanks. If u don't mind I also want to figure out why panel.center isn't working. ```super.viewDidLoad() let panel = UIView() panel.frame = CGRect(x: 0, y: 400, width: 400, height: 200) view.addSubview(panel) let shapeLayer = CAShapeLayer() let CircularPath = UIBezierPath(arcCenter: panel.center, radius: 300, startAngle: 0, endAngle: 2*CGFloat.pi, clockwise: true) shapeLayer.path = CircularPath.cgPath panel.layer.addSublayer(shapeLayer) } ``` – whitejeremy Aug 05 '20 at 09:05
  • what is the issue with that? – Johnykutty Aug 05 '20 at 09:14
  • What are you trying to achive?, as a suggestion, please avoid hard coding the frame it may go wrong in different screen sizes – Johnykutty Aug 05 '20 at 09:17
  • it draws the circle off the screen as before – whitejeremy Aug 05 '20 at 09:18
  • draw the circle in viewdidlayoutsubviews not on viewdidload , you get proper centre – nca Aug 05 '20 at 10:36
  • 1
    What worked was letting center = CGPoint of the midpoints of the bounds for the panel. thanks everyone for the help. – whitejeremy Aug 05 '20 at 11:50