Take the width and height of the device

I am using this code:

CGRect myRect = CGRectMake(self.frame.size.width, self.frame.size.height);

But he gives this error:

- Property "frame" not found on object of type [myViewController]

Anyone have any ideas on how to change the code?

+3
source share
3 answers

First of all, you have too few arguments for the function CGRectMake. You must determine your origin. In addition, selfyou need to have instead self.view. It looks something like this:

CGRect myRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
+8
source

the presentation frame is executed from inside the code

  CGRect myViewFrame = self.view.frame;

or screen frame if that is what you want (the device says in the question header)

CGRect screenFrame = [[UIScreen mainScreen] bounds];

or it will return the same status bar minus if visible

CGRect actualyScreenFrame = [[UIScreen mainScreen] applicationFrame];
+6
source

Use

CGRect myRect = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.heigh);

And if you want to know the screen size of the device, you should use:

[UIScreen mainScreen].bounds
+2
source

Source: https://habr.com/ru/post/1793653/


All Articles