Rizon
Rizon

Reputation: 1546

Auto-resize UIView to fit iPhone 5 screen while maintaining proportion

My StoryBoard is configured for iPhone4 resolution, and when running on iPhone 5 I'd like a UIView to get bigger (both height&width).

The problem right now is that the view is only getting higher and not wider. What should be the auto-resize configuration in order to achieve this?

enter image description here enter image description here

Upvotes: 0

Views: 1320

Answers (2)

bachonk
bachonk

Reputation: 3954

Reference the height of the screen and use some padding values to set your view frame. Below is code to set the frame of a UIView called yourShapeView:

// Get the frame of the screen
CGRect screenFrame = [[UIScreen mainScreen] bounds];

// Set padding from the top and bottom of the shape
CGFloat verticalPadding = 40.0f;
CGFloat horizontalPadding = 20.0f;

// Get the height/width values
CGFloat height = screenFrame.size.height - (verticalPadding * 2);
CGFloat width = screenFrame.size.width - (horizontalPadding * 2);

// Set the size of your shape based on the height and padding
yourShapeView.frame = CGRectMake(horizontalPadding, verticalPadding, width, height);

Upvotes: 1

Undo
Undo

Reputation: 25687

You will probably need to use a subclass of UIView with the setFrame: method overridden to catch frame changes.

- (void)setFrame:(CGRect)frame
{
    frame.size.width = frame.size.height; // Make the *width* always equal to the *height*. Logic could go here, etc.
    [super setFrame:frame]
}

Upvotes: 1

Related Questions