Get actual iOS sizes

How do I get the actual dimensions of a view or subview that I control? 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 is larger than the view to which it is being added.

With this thinking (if many objects are added as sub-items), how can I determine the actual width and height of the firstView ?

+7
source share
3 answers

If you want the button to have the same width and height as the firstView, you should use the bounds property of the UIView as follows:

 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 the firstView, you can do something like this:

 CGSize size = firstView.bounds.size; NSInteger width = size.width; NSInteger height = size.height; 
+4
source

The first view is actually 200 x 100. However, views inside are not cropped unless you use clipsToBounds .

The preferred solution to your problem is to stop adding views that exceed dimensions, or to stop resizing so that they fill up the available space as needed.

(To really calculate the full boundaries, including all subviews in the hierarchy, it’s not difficult, just a lot of work that really won’t take you anywhere. UIView allows UIView to resolve views more than their containers, but this does not necessarily give any guarantees as to that will work properly and is likely to be the source of errors, if you use them. The reason for clipsToBounds probably lies in the fact that the cut-off is expensive, but necessary during the animation when subventions can temporarily move from ranitsy for effect purposes before they are removed from the view.)

0
source

firstview.frame.size.width firstview.frame.size.height

0
source

All Articles