Reputation: 23359
I have a UIBarButtonItem
which is just an image, I wanted to use this image as the background image of a bar item with an empty string title.
But the width didn't work out correct, in order to have the correct dimensions, I made a UIBarButtonItem
with a custom view which was a UIButton
with the image set.
The problem I have is that in landscape mode this custom view doesn't resize to fit correctly the smaller navigation bar.
I tried making my button autoresize by allowing flexible height, but it's preserving the top and bottom margins of the portrait and now my button is very squashed.
The reason I originally wanted to use a bar item with empty title was to use the appearance protocol to set the background image for bar metrics default and landscape to bypass this problem.
How can I make my UIBarButtonItem
support portrait and landscape sizes with a custom view?
I have a dedicated landscape image so that it's just smaller, not distorted which I want to use.
Portrait:
Landscape:
Landscape with autoresizing, note the margins from portrait are causing an exaggerated squashing of the bar button.
Upvotes: 4
Views: 1395
Reputation: 51
The best answer is to provide two images one for portrait mode and the other is for the landscape mode and your code will be like
UIBarButtonItem *barBtnLeft= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"yourImg.png"] landscapeImagePhone:[UIImage imageNamed:@"yourImg_Landscape.png"]style:UIBarButtonItemStyleBordered target:self action:@selector(yourSelector:)];
I know this post is old, but it luck the right answer, I hope newcomer, like me, will get the help with this solution.
I should mention here that landscape image should be smaller by 0.75f scale.
Upvotes: 2
Reputation: 587
If you just want it to maintain it's aspect ratio, and the custom view you've used is a UIImageView (which I assume it is), you can set the contentMode property of the image view to make it scale while maintaining aspect ratio. You're probably gonna want to use UIViewContentModeScaleAspectFit
.
Upvotes: 0
Reputation: 587
Is the image you're using resizable? Did you create it with - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets
?
Upvotes: 0