Working with autorotation and UINavigationBar images - ios

I have a navigation bar that uses a custom image for the background. Along with some images for buttons UIBarButtonItems.
When the device is rotated the navigation bar height is obviously changed to 32px instead of 44px.
Is it best practice to then make another image to be used for the navigation bar background or create stretchable images instead?

I would create two stretchable images one for retina an one for default devices, it's is best practice to use stretchable images instead of full length images because you decrease the app size and there will be no problems if the device screen size is changed (if the stretchable image is seted properly)

Related

UITabBar buttons' images wrong size

I have a UITabBarController.
I added images to the bar buttons. On the ViewController the image size is fine, but on the TabBarController it looks all messed up.
Furthermore, when I fire up the app, and click the tabs - the images change size. I read online that I need to set the inset to be, for example:
Top: 5, Bottom: -5
But then the image isn't the size I want it to be, and it overlaps the text I want it to also have.
See image please.
How do I fix that? I am using Swift.
As per Apple Human Interface Guidelines, Tab Bar Icon size varies for different size glyphs and for portage and landscape mode, refer the guide below and check if the icon size is matching the size given. Make sure to verify #2x and #3x image icon sizes and add them if missing the proper size.
You should set images with correct size - 32x32, because UITabBarController does not fit images automatically.
For resizing images you can use something like this: https://github.com/ymedialabs/UIImageCategories/blob/master/UIImage%2BvImage.swift

How to use asset catalog for navigation bar background image?

I use asset catalog with 2x images for iPhone5 and 6 as described in docs:
Image for navigation bar background is 750x128 (750 x 44 * 2 + 20 * 2) because I want also to cover status bar. 750 is iPhone6 resolution width. Bg image setting:
self.navigationController?.navigationBar
.setBackgroundImage(UIImage(named: "nav_bar_bg"), forBarMetrics: .Default)
Let's look at result:
OK. iPhone5:
Not OK. How can I downscale it without removing background image from asset catalog? I really don't want to hardcode it. It's really annoying that iPhone5 and 6 resources can't be separated.
Any suggestions? Thanks
Sorry, didn't notice the UINavigationBar thing... You can change the resize of the picture that is used in the background by making your picture stretchable. That way you can use a picture that fits the smaller devices (the iPhone5 in your example) and keep the picture centered in the iPhone 6.
Another more convoluted solution would be to subclass the NavBar altogether and rewrite the way the picture is drawn.
You should use NSLayoutConstraints (leading, top and trailing), and set the UIImageView content to Fit. That way you won't need to change the image, it will be automatically scaled by the application.

Image Automatically Resized In Run-time But Not On Interface Builder

Alright, this is struggling me by now. I have a PNG image which has the size of 10x36px.
When I try to use that in any of my views it gets resized. Not the frame property, this one remains intact. But visually the image is not the same.
The real image is like this . (Sorry if you can barely see it. That's because it has a white background on a white foreground).
But when I am in the simulator (it happens on a device too) it looks like this.
.
I'm not using contraints(I turned off AutoLayout in order to find the bug).
I printed the frame of that button and it's width, height, x and y values are just as they are in the interface builder. And I printed them everywhere and in response to different events too(I know, that's not the reason. I'm just desperate).
This happens only with this PNG specifically cause other UIButton I have in the very same view, but with differents Images, are working just fine.
What could be the cause of this error?
EDIT:
Trying as a background image it got better. But still not perfect.
Place the png image inside an empty 36x36 png image in Photoshop/Pixelmator to stop it resizing and preserve the original ratio.
Or try setting the views content mode to "aspect fit" in Interface Builder or programmatically:
yourImageView.contentMode = UIViewContentModeScaleAspectFit;
The cause could be that UIBarButtons treat images differently than UIButtons. UIBarButtons are typically used in UINavigationBars or UIToolbars while UIButtons are used typically in the View.
Make sure you are using Button background image and not Button image.
Button background image covers the whole button background.
Button image places the images on a portion of the background.
Also, use xcassets. It comes with the project template. If you don't have it (deleted it), add a new one and create an image set. Once you do that, add your images. Make sure to select "Render as original image" for each image in your image set.
The PNG image you linked to isn't 10x36 pixels, but 10x30. Oops?

Center UIButton image with UIBarButtonItem on UIToolbar

I'm trying (everything in XCode with Interface Builder) to create a UIButton with image adding it to UIToolbar, which makes it UIBarButtonItem (when click twice it shows the UIButton properties).
I have a image#2x.png image with size 64x88 (height is 88, because in Retina it's the toolbar height). I'm not interested in non-retina screens. I added this image to UIButton setting Mode in View properties to Center (or Aspect Fit, it shouldn't really matter here; tried both, aswell).
Set its size to 32x44 (44 height in builder). Then, dragged it to UIToolbar.
Now, I want to change the width of my button to make it larger, because I want to give user some more space to tap, also, I planned the toolbar background for that. And I want the image on this button to remain the same, but centered. But, when I try to resize the UIBarButtonItem, it actually scales the image, no matter what Mode is set.
That's what happens (I'm showing an actual dragging process):
However, if I change image real size to height 44, it works well. But 44 height looks ugly in Retina devices, so, what should I do?
I've seen this happen. For the sake of argument, create two images, image.png and image#2x.png, and when setting the image of the button, select image.png in Xcode.
Alternatively, you can create a UIView-based UIBarButtonItem and have a UIImageView and a UIButton. This way, you have a more precise control over the image sizing.

UIBarButtonItem support portrait and landscape sizes with a custom view?

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.
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.
Is the image you're using resizable? Did you create it with - (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets?
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.

Resources