Linuxmint
Linuxmint

Reputation: 4746

Grab the width and height of a device

I am using this code:

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

But it gives this error:

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

Anyone have any ideas on how I should modify my code?

Upvotes: 4

Views: 3580

Answers (3)

Max
Max

Reputation: 16709

Use

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

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

[UIScreen mainScreen].bounds

Upvotes: 2

Zaky German
Zaky German

Reputation: 14334

the view frame of the view from within the code is being executed

  CGRect myViewFrame = self.view.frame;

or the frame of the screen if that's what you want (says device in the title of the question)

CGRect screenFrame = [[UIScreen mainScreen] bounds];

or this which will return the same minus status bar if visible

CGRect actualyScreenFrame = [[UIScreen mainScreen] applicationFrame];

Upvotes: 6

sudo rm -rf
sudo rm -rf

Reputation: 29524

First of all, you have too few arguments for the CGRectMake function. You need to define your origin. Also, you need to have self.view instead of just self. Looks something like this:

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

Upvotes: 9

Related Questions