0

I´ve been reading the different questions ( Cocoa: What's the difference between the frame and the bounds?, UIView frame, bounds and center) related to the difference between frame and bounds but still I don´t understand why when something like this:

UILabel *newMark = [[UILabel alloc] initWithFrame:self.frame];
newMark.text = @"|A3";
[self addSubview:newMark];

or this:

UILabel *newMark = [[UILabel alloc] init];
newMark.text = @"|A3";
newMark.frame = self.frame;
[self addSubview:newMark];

The label is not displayed, but when doing the equivalent with the bounds, like this:

UILabel *newMark = [[UILabel alloc] initWithFrame:self.bounds];
newMark.text = @"|A3";
[self addSubview:newMark];

or this

UILabel *newMark = [[UILabel alloc] init];
newMark.text = @"|A3";
newMark.frame = self.bounds;
[self addSubview:newMark];

it is displayed. I wouldn´t have a problem with using the bounds, but I think is not placing the label on the right place, as it can be seen on the following screenshot:

Label

Where, as you can see "|", "A" and "3" are cut because they are displayed more at the bottom than the screen of the ipad. Any ideas?

Community
  • 1
  • 1
AlvaroSantisteban
  • 5,256
  • 4
  • 41
  • 62

1 Answers1

1

The frame is relative to its superview. So if your frame's origin is at (100,200) within your superview, and you set a subview's frame to be the same as your frame, then your subview will be at (100,200) within you.

Typically (provided there aren't transforms involved), when you want to tell a subview to be the same size as the superview, you say:

subview.frame = superview.bounds;
Rob Napier
  • 286,113
  • 34
  • 456
  • 610