Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get actual dimensions of iOS view

How would I get the actual dimensions of a view or subview that I'm controlling? For example:

UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,100)];
[self addSubview:firstView];

UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(10, 20, 230, 120)];
[firstView addSubview:button];

As you can see, my button object exceeds the dimensions of the view it is being added to.

With this is mind (assuming there are many objects being added as subviews), how can I determine the actual width and height of firstView?

like image 996
GeorgeBuckingham Avatar asked Feb 13 '13 13:02

GeorgeBuckingham


1 Answers

If you would like to have the button have the same width and height as firstView you should use the bounds property of UIView like this:

UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,100)];
[self addSubview:firstView];

UIButton *button = [[UIButton alloc] initWithFrame:firstView.bounds];
[firstView addSubview:button];

To just get the width/height of firstView you could do something like this:

CGSize size = firstView.bounds.size;
NSInteger width = size.width;
NSInteger height = size.height;
like image 127
Soph Avatar answered Nov 09 '22 19:11

Soph