0

I am trying to draw graphs so they look the same on all devices. To do this, I have first found the width of my view (which has been set in AutoLayout to be half the width of the superview). Then in code I have set a variable unitDim to be one thousandth of that width:

func setDimension() {
    unitDim = Float(self.bounds.width)/1000
}

Then, instead of drawing in terms of points, I have used unitDim. e.g. axisLength = 400 * unitDim. I have set the values of these in a function setDimensions() which is called from the view controller for that view. So my code in the draw function is as shown below: code from draw function

So I would expect my axis to take up 40% of the view width. But when I view the axes in iPad and iPhone I get different results. I cannot figure out why this is. Below is how it appears on iPad simulator:

iPad view

and below is how it appears on iPhone simulator:

iPhone view

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Steblo
  • 625
  • 2
  • 12
  • 22
  • 1
    Please don't use screenshots of code in your question. Just copy and paste the code and format it to be rendered as code. It makes your code searchable and easier to read. – Fogmeister Oct 07 '16 at 14:37
  • sorry. will bear that in mind. – Steblo Oct 07 '16 at 15:04
  • At what point are you running the `setDimension` method? – Fogmeister Oct 07 '16 at 18:42
  • I have found an answer here:[getting current width and height with auto layout](http://stackoverflow.com/questions/13446920/how-can-i-get-a-views-current-width-and-height-when-using-autolayout-constraint) – Steblo Oct 08 '16 at 09:25
  • my reason for Adkin I that I assume you were calling the setDimension method from viewDidLoad? By moving it to viewWillAppear it would have fixed the issue. – Fogmeister Oct 08 '16 at 11:40
  • Actually I did try that. But I suspect it may have been something to do with the fact that my view is set in Auto-layout to be a proportion of screen width. – Steblo Oct 14 '16 at 18:52

0 Answers0