Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CGRectMake in Objective-C

Tags:

objective-c

I am using CGRectMake to produce a label and a textField inside of a UITableViewCell. Here is the code:

   label = [[UILabel alloc] initWithFrame:CGRectMake(20, 10, 0, 20)];
   label.backgroundColor = [UIColor redColor];      

   textField =
     [[UITextField alloc] initWithFrame:CGRectMake(0, 10, 0, 20)];
   textField.backgroundColor = [UIColor blueColor];

  [textField setDelegate:self]; // set the delegate to this object
  [textField addTarget:self action:@selector(textFieldDidEndOnExit)
      forControlEvents:UIControlEventEditingDidEndOnExit];

  [self.contentView addSubview:label]; // add label to the cell
  [self.contentView addSubview:textField]; // add textField to cell

First, my understanding is the the parameters are defined as

  CGRectMake(x, y, width, height) 

What I see in the cell is a label on the left with the textField immediately to its right. There is a small space between the two.

I would expect the X,Y coordinates to be relative to a 0,0 point in the upper left of the cell, with positive values going down and to the right.

I am confused by what I see.

I see that the label is located starting at the left of my cell, but it has an X value greater than the X value of the textField. The textField starts to the right of the label.

I don't understand why the width value for each is set to 0 (zero).

Also, what makes the textField take a position relative to the label, rather than relative to the cell coordinates.

Here is a simple sketch of the cell and what I see in it (with the background colors set, I can clearly see where the CGRects are being placed):

0,0---------------------------------------------------------------------------------
|                                                                                   |
|   ----------------    -----------------------------------------------------------------
|  |                |  |    
|  |  label         |  |   textField 
|  |                |  |    
|   ----------------    -----------------------------------------------------------------
|                                                                                   |
------------------------------------------------------------------------------------

I also notice that the width of each label varies according to its content length. I understand this, but I don't understand why the textField moves around, staying the same distance from the label in each case.

[edit] By the way, "self" is a subclass of UITableViewCell, if I didn't make that clear.

like image 326
Jim Avatar asked Apr 12 '11 04:04

Jim


People also ask

What is CGRectMake?

CGRectMake(_:_:_:_:)Returns a rectangle with the specified coordinate and size values.

What is CGSize Swift?

A CGSize structure is sometimes used to represent a distance vector, rather than a physical size. So CGSize is used in drag operations to represent the vector of the drag in terms of x distance (width) and y distance (height).


1 Answers

enter image description here

like image 62
Aravindhan Avatar answered Nov 16 '22 02:11

Aravindhan