Comparing two CGRects

Tim Vermeulen picture Tim Vermeulen · Oct 14, 2012 · Viewed 29.7k times · Source

I needed to check wether the frame of my view is equal to a given CGRect. I tried doing that like this:

CGRect rect = CGRectMake(20, 20, 20, 20);
if (self.view.frame == rect)
{
    // do some stuff
}

However, I got an error saying Invalid operands to binary expression('CGRect' (aka 'struct CGRect') and 'CGRect'). Why can't I simply compare two CGRects?

Answer

Amelia777 picture Amelia777 · Sep 16, 2013

Use this:

if (CGRectEqualToRect(self.view.frame, rect)) {
     // do some stuff
}