简体   繁体   中英

Changing just 1 value in a view frame

I am somewhat new to iOS and to change the width or height of a view I am using the following code:

view.frame = CGRectMake(view.frame.origin.x,
                        view.frame.origin.y,
                        view.frame.size.width,
                        newHeight);

This feels wrong because I am copying all of the old values into a new rect just to change a single value, but I can't just do:

view.frame.size.height = newHeight;

Is there a better way to do this?

You can't assign the values directly. You have to create a variable for it:

CGRect frame = view.frame;
frame.size.height = newHeight;
view.frame = frame;    // or [view setFrame:frame];

Another way which helps you keep the frame variable in a different scope:

view.frame = ({
    CGRect frame = view.frame;
    frame.size.height = newHeight;
    frame;
});

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