Ronald Hofmann
Ronald Hofmann

Reputation: 1430

Wrong coordinates for UIButton

On a scene I have a UIButton _btnBuffalo (beside others) with these coordinates in the utilities inspector:

enter image description here

In my ViewController I´m trying to get the buttons rect like this:

 CGRect btnRect = _btnBuffalo.bounds;

Unfortunately I´m getting this result with NSLog:

btnRect x: 0.000000,  y: 0.000000,  w: 160.000000,  h: 120.000000

I really understand why x and y are 0.000000, any ideas?

Upvotes: 0

Views: 260

Answers (2)

mas'an
mas'an

Reputation: 160

You should call _btnBuffalo.frame instead of _btnBuffalo.bounds. bounds always returns zero origin for any view.

Upvotes: 3

wesley
wesley

Reputation: 859

The frame property specifies the size and location of the view in its superview’s coordinate system.so you have to do like this.

CGRect btnRect = _btnBuffalo.frame;
NSLog(@"%f ,%f ,%f ,%f",btnRect.origin.x,btnRect.origin.y,btnRect.size.width,btnRect.size.height);

Upvotes: 1

Related Questions