Why can't I access the visibleRect Property of an CALayer in iPhone SDK?
Question
I am having trouble doing this:
CALayer *myLayer = myUIImageView.layer; //works, no error from compiler
CGRect visRect = myLayer.visibleRect; //fat error, see below
The Error I get is:
error: request for member 'visibleRect' in something not a structure or union
but the documentation says:
visibleRect Returns the visible region of the receiver, in its own coordinate space. (read-only) @property(readonly) CGRect visibleRect
I have included QuartzCore.framework, CoreGraphics.framework, UIKit.framework, Foundation.framework.
So if that returns a CGRect, why doesn't it work? Any idea?
Solution
I'm 99% sure you are missing the import statement. It's not enough to just include the framework in your project.
I just tried this on a brand new project and it worked as expected. The following is what I added to the ViewController.
#import <QuartzCore/QuartzCore.h>
...
- (void)viewDidLoad
{
[super viewDidLoad];
CALayer *test_layer = self.view.layer;
CGRect test_rect = test_layer.visibleRect;
NSLog(@"%f, %f", test_rect.origin.x,
test_rect.size.width);
}
output was:
2009-04-21 17:53:55.999 Throwaway[72422:20b] 0.000000, 320.000000
OTHER TIPS
Your code looks correct. You aren't retaining anything, which could be dangerous, but technically isn't wrong.
the below message usually means that something is wrong with myLayer.
error: request for member 'visibleRect' in something not a structure or union
Are you doing anything inbetween the two lines of code?
Inspect the myLayer object, make sure it's correct. Also, confirm myUIImageView is initialized and not nil.
Also, try bypassing properties, by calling [myLayer visibleRect];