views:

538

answers:

2

Hi, I'm relatively new to objective-c...I'm using the iphone 3.0 SDK

I have a UIView, which is a subview, that I want to resize under certain circumstances.

The way I do it is within a controller class.

for example,

CGSize el = CGSizeMake(30, 40);
[self.subview setSize:el];

the above code does work, but the compiler gives a warning: 'UIView' may not respond to 'setSize:'

At some level, "if it ain't broke, I don't want to fix it", but I'm a little worried that I'm doing something wrong.

Any ideas as to why I'm getting the warning and how I can fix it?

TIA

+2  A: 

That probably means that setSize for UIView is implmented but it is not shown in the header file that is imported into the project. That makes it an undocumented API, ie one day it could change and break your code :)

And sure enough if you go to the documentation of UIView you will find no refrence to the size property. So I would avoid it.

What you should use instead is the frame property

CGSize el = CGSizeMake(30, 40);
CGRect bounds = CGself.subview.bounds;
bounds.size = el;
CGself.subview.bounds = bounds;

Give that a shot.

hhafez
The `frame` method returns the frame by value, so you can't set its size like that. You'd need to do something like `CGRect frame = self.subview.frame; frame.size = CGSizeMake(30, 40); self.subview.frame = frame;`.
Chuck
I just checked the documentation and you are correct
hhafez
A: 

The right thing to do here is use something else instead of the non-public size property. But for the sake of discussion: If you wanted to get rid of the warning, you can declare that you know about the size property somewhere at the top of your implementation file:

#import "MyClass.h"

@interface UIView (private)
- (void) setSize: (CGSize) newSize;
@end

@implementation MyClass
…
@end

The compiler will stop complaining.

zoul