Chris
Chris

Reputation: 1015

Stretching a UIToolbar background image when rotating to landscape

I am adding an image to a UIToolbar's background in the App Delegate using this code:

[[UIToolbar appearance] setBackgroundImage:
[UIImage imageNamed:@"toolbarbackground.png"] 
                             forToolbarPosition:0 barMetrics:UIBarMetricsDefault];

The image is 768 pixels in width (the size of the iPad screen width in portrait orientation). When I rotate to landscape the image does not stretch to fill the new screen width (1024 pixels). How can I use a custom background image on a UIToolbar that stretches to fit the screen when rotating?

Upvotes: 2

Views: 1888

Answers (2)

flypig
flypig

Reputation: 1259

UIImage *navBarImagePortrait = [UIImage imageNamed:@"navbar-portrait"];
navBarImagePortrait = [navBarImagePortrait resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 0, 2)];
UIImage *navBarImageLandscape = [UIImage imageNamed:@"navbar-landscape"];
navBarImageLandscape = [navBarImageLandscape resizableImageWithCapInsets:UIEdgeInsetsMake(1, 0, 0, 2)];

// custom NaviagionBar
[self.toobar setBackgroundImage:navBarImagePortrait forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsDefault];
[self.toobar setBackgroundImage:navBarImageLandscape forToolbarPosition:UIToolbarPositionAny barMetrics:UIBarMetricsLandscapePhone];

Upvotes: 1

George
George

Reputation: 4029

Does the toolbar autoresize? If not , set it's autoresizing mask:

[myToolbar setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleRightMargin]

If the toolbar does autoresize and the image still doesn't , set the contentMode of the image view to UIViewContentModeScaleToFill.

Upvotes: 2

Related Questions