3

I'm trying to create a circle using CGPoint and CGFloat. But I want the circle to be different in siz based on which device is used. I changed all my Size classes to reflect the sizes I want and I was hoping, it would recognise that but it doesn't.

WhiteBar is a UIView within my main view that I created using SB and to test that my view is changed in the size class, I set a background color and saw its set as I want it

my print line shows me the smallest width and height

@IBOutlet var WhiteBar: UIView!
var WhiteCircle = CAShapeLayer()
WhiteCircle.strokeColor = UIColor.whiteColor().CGColor
WhiteCircle.fillColor = UIColor.clearColor().CGColor
WhiteCircle.lineWidth = 15
WhiteCircle.strokeStart = 0
WhiteCircle.strokeEnd = 1

let WhiteCenterPoint = CGPoint (x: WhiteBar.bounds.width / 2, y: WhiteBar.bounds.height / 2)
let WhiteCircleRadius : CGFloat = (WhiteBar.bounds.height / 2 -  WhiteCircle.lineWidth / 2)

println(WhiteBar.bounds.width) // outputs the smallest size of all my size classes
println(WhiteBar.bounds.height) // outputs the smallest size of all my size classes

var WhiteCirclePath = UIBezierPath(arcCenter: WhiteCenterPoint, radius: WhiteCircleRadius, startAngle: CGFloat(-0.5 * M_PI), endAngle: CGFloat(1.5 * M_PI), clockwise: true)

WhiteCircle.path = WhiteCirclePath.CGPath

WhiteBar.layoutIfNeeded()

WhiteBar.layer.addSublayer(WhiteCircle)

EDIT: It seems the size is set based on the w: Any h: Any size class.

Thanks in advance, Ace

Ace Green
  • 381
  • 5
  • 29
  • Where is this code you have posted placed? – bjtitus Mar 08 '15 at 14:49
  • In func TrackProgress() which I call in my func viewDidLoad() – Ace Green Mar 08 '15 at 17:47
  • possible duplicate of [scrollview size with Autolayout: frame is 600x480 in compact/Any size class, bigger than iphone5?](http://stackoverflow.com/questions/28703078/scrollview-size-with-autolayout-frame-is-600x480-in-compact-any-size-class-big) – bjtitus Mar 08 '15 at 18:27
  • possibly!!! Anyways I got my CAShapeLayer to work and this can be closed – Ace Green Mar 08 '15 at 18:49

1 Answers1

0

Size classes (or any auto layout changes, for that matter) are not applied until after viewDidAppear or viewDidLayoutSubviews. You will need to move this code into one of those. You are seeing the bounds of the view as it is loaded out of the Interface Builder document (xib or storyboard). You should never be checking frames or bounds until after those lifecycle events.

https://stackoverflow.com/a/28704658/72319

Community
  • 1
  • 1
bjtitus
  • 4,231
  • 1
  • 27
  • 35
  • Haha I went through your profile and came across that post already :) But My fun TrackProgress is called within my viewDidLoad so its that the same? – Ace Green Mar 08 '15 at 17:55
  • Yes, viewDidLoad happens first of the view lifecycle methods. – bjtitus Mar 08 '15 at 18:00
  • No, any code checking the status of views post auto layout must happen after viewDidAppear or viewDidLayoutSubviews are called (that can be inside of them). – bjtitus Mar 08 '15 at 18:04
  • "that can be inside of them" within is inside of them. So it CAN or CAN NOT be inside viewDidLoad? if so, where can I put it, if I want it to be available when the view loads – Ace Green Mar 08 '15 at 18:06
  • Look at this diagram: https://www.safaribooksonline.com/library/view/learning-xamarin-studio/9781783550814/ch06s02.html constraints aren't applied until viewDidAppear/viewDidLayoutSubviews – bjtitus Mar 08 '15 at 18:10
  • Ok so I tried putting it in the override func viewWillAppear(animated: Bool) { super.viewWillAppear(false) TrackProgress() } And the result is the same. It only considers the w: any h: any size class which has no constraints on that subview since it changes in each size class – Ace Green Mar 08 '15 at 18:16
  • I told you that it only happened after viewDidAppear viewWillAppear is clearly before that. – bjtitus Mar 08 '15 at 18:20
  • How? The arrows are point down. Init -> loadView -> viewDidLoad -> viewWillAppear – Ace Green Mar 08 '15 at 18:22
  • Nothing above viewDidAppear will contain the proper frames. – bjtitus Mar 08 '15 at 18:24