0

I am trying to retrieve rect value of a class in ViewDidAppear. The button is in UITableViewCell. All values return correct except frame.origin.y. It returns -1.5. I am wondering what might cause that.

CGRect customRect = CGRectMake(self.favoriteButton.frame.origin.x, self.favoriteButton.frame.origin.y, self.favoriteButton.frame.size.height, self.favoriteButton.frame.size.width) ;
birdcage
  • 2,638
  • 4
  • 35
  • 58

3 Answers3

2

Origin.y is defined in an unintuitive.

First, moving down is positive and up is negative (aka larger numbers means the view will appear lower on the screen).

Second, the origin is relative to the top left corner of the immediate parent view (in your case the cell). If origin.y really is -1.5 then the top of your button is just one and a half points above the top of your table view cell, (which sounds likely). If your origin.y really should be 400-ish points it means for it to appear in your UITableViewCell your tableViewCell would have to be over 400 points tall.

This could be the case but I'm wondering if what you're looking for is not origin.y, but how far bellow the top of the root view (self.view in your view controller) the top of your button is. If so try:

CGPoint originInRootview = [self.view convertPoint:CGPointZero fromView:self.favoriteButton];
CGFloat theNumberIThinkYouWant = originInRootview.y
TMin
  • 2,280
  • 1
  • 25
  • 34
  • @birdcage my bad, the code I had would have converted the frames to the root.view's coordinate system, which is wrong because you don't want the frame's origin (it's location relative to the tableViewCell) converted into the rootViews coordinate system. You want the top left corner (0,0) of your button in terms of the root view's coordinate system. I've updated the code. Here's a link to a similar answer that may be helpful https://stackoverflow.com/questions/8465659/understand-convertrecttoview-convertrectfromview-convertpointtoview-and – TMin Oct 03 '17 at 17:25
  • `the origin is relative to the top left corner of the immediate parent view` - This is what i was missing. I thought it was from the superview. Thanks!! – Manoj Oct 28 '21 at 09:47
  • @Manoj the superview is the immediate parent view. perhaps you were confounding parent view and root view? – TMin Oct 28 '21 at 16:39
0

There are two things

1-) You said that it is in TableViewCell so it returns correct. Because you put button inside a view(TableViewVell container view), button gets it coordinates from container view not from superview.

2-) If TableViewCell is big enough and starts from y=0 and you are sure it is wrong and must be something like 400. Try to get the rect value at viewDidLayoutSubviews

Clown
  • 163
  • 1
  • 12
0

All values return correct except frame.origin.y. It returns -1.5. I am wondering what might cause that.

Applying a transform to the view can cause it's frame property to be invalid. From the documentation for UIView's frame property:

Warning

When the value of this property is anything other than the identity transform, the value in the frame property is undefined and should be ignored.

So, if you're not seeing what you expect, compare your view's transform to CGAffineTransformIdentity.

Caleb
  • 124,013
  • 19
  • 183
  • 272