2
votes

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?

2

2 Answers

2
votes

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.

1
votes
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];