简体   繁体   中英

how can i make view auto resize to fit to superview

i have 2 views

first is view1 size is 100x100

second is view2 size is 200 x200

my code is

[view1 addSubview:view2];

but size of view2 is greater than view1

how can i make view2 smaller to fit size of view1

thank you

The modern way to achieve this is using auto layout. That way, no matter how your view rotates or changes, they'll stay the same size.

Depending on your setup, it'd be something like this

NSLayoutConstraint *widthConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeWidth multiplier:1.f constant:0.f];

NSLayoutConstraint *heightConstraint = [NSLayoutConstraint constraintWithItem:view1 attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:view2 attribute:NSLayoutAttributeHeight multiplier:1.f constant:0.f];

[view1 addConstraints:@[widthConstraint,heightConstraint]];

If you need to target an earlier OS, set the frames equal and use autoresizingMask .

When you want to fit bigger subview in smaller superview then:
1. Make size of both the views same/equal.
2. Make origin for subview as (0,0). As frame property takes view's location in its superview's coordinate system so for subview origin will be (0,0).

For your case you can do something like this:

UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, view1.frame.size.width, view1.frame.size.height)];  
[view1 addSubview:view2];

OR

UIView *view2 = [[UIView alloc] initWithFrame:view1.bounds];  
[view1 addSubview:view2]; 

This is really beneficial when you want to shrink your bigger images (UIImageView) in smaller superviews (UIView).

from UIView Class Reference

Views can embed other views and create sophisticated visual hierarchies. This creates a parent-child relationship between the view being embedded (known as the subview) and the parent view doing the embedding (known as the superview). Normally, a subview's visible area is not clipped to the bounds of its superview, but in iOS you can use the clipsToBounds property to alter that behavior. A parent view may contain any number of subviews but each subview has only one superview, which is responsible for positioning its subviews appropriately.

and from frame property reference

The frame rectangle, which describes the view's location and size in its superview's coordinate system.

and from bounds property reference

The bounds rectangle, which describes the view's location and size in its own coordinate system
.

So you should setup your view1.frame or your view2.bounds

[view1 setBounds:CGRectMake(`x`,`y`,`width`,`height`)];

or

[view2 setFrame:CGRectmake(`x`,`y`,`width`,`height`)];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM