简体   繁体   中英

How to resize UIView in iOS?

I have got a main view and a view1 into main view, how to resize view1?

This code doesn't work:

self.View1.bounds.size.height = self.view.bounds.size.height;
CGRect frame = self.View1.frame;
frame.size.height = self.view.bounds.size.height;
self.View1.frame = frame;

You can't modify the bounds directly. The CGRect needs to be modified outside of the view's bounds.

CGRect viewBounds = self.view1.bounds;
viewBounds.size.height = self.view.bounds.size.height;
self.view1.bounds = viewBounds;

Hope this helps!

[self.View1 setFrame:CGRectMake(self.view.frame.origin.x, self.view.frame.origin.x, self.view.frame.size.height, self.view.frame.size.height)];

I don't have enough reputation to add a simple comment to some of the other answers, so I'll help point out a couple of important points here:

(1) If just changing the size, set new bounds instead of frame

If you are just looking to resize an image (but keep the image at the same location), then follow cdownie's suggestion of setting new bounds instead of a new frame . For example, if you have an image of a pointer hand and want to make it look as if it's tapping the screen, then scaling the size of the bounds down and back up real quick will achieve that and it will maintain its same screen location at its center point if you set the anchor point of the image at its center. If you had changed the frame instead as Tony Friz suggested, then upon a reduction of image size, the image will pull to its top-left corner (unless you correct for that with extra math).

(2) If the image created in nib or storyboard, turn off Autolayout and Autosizing

Autolayout needs to be turned OFF over any views in which you are moving images around or resizing them. If you leave it on, the behavior will (of course) be odd and unpredictable. Also, with Autolayout off, go into the image's Size Inspector and set the proper origin (center, for example) and disable all Autosizing constraints by clicking on any solid lines in the Autosizing grid until all of them are dotted lines. This way you will have full control over image placement by altering the center property and sizing by setting new bounds .

Hope this helps, Erik

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