user141146
user141146

Reputation: 3315

xcode objective-c warnings

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

Upvotes: 1

Views: 3395

Answers (3)

MLBDG
MLBDG

Reputation: 1367

Here is a closer explanation using the "frame" property for "myView":

[self.myView.frame = CGRectMake(x, y, width, height)];

Where:

  • x, coordinate FOR the top left corner of your view having as reference the top left corner of its parents "x" coordinate.
  • y, same as x but y axis
  • width, horizontal size of the frame
  • height, vertical size of the frame

i.E. You have a view which fits to the screen bounds, so its coordinate (0,0) will be the same as your device screen top left corner. if you want to add a subview barely smaller that the screen size and center it horizontally and vertically, here is the set up:

[self.myView.frame = CGRMake ( 20 , 20 , self.view.frame.size.width-40, self.view.frame.size.height-40);

This example sets the frame inside the view and centered. Note that we subtract 40 to the width corresponding to: 20 left side, 20 right side, and so the same for vertical adjustments.

This code will also work in portrait and landscape mode.

Upvotes: 0

hhafez
hhafez

Reputation: 39750

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.

Upvotes: 3

zoul
zoul

Reputation: 104065

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.

Upvotes: 0

Related Questions