Search code examples
iosviewframecgrect

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?


Solution

  • 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];