TabBarItems and setting their image sizes?
You should prepare 3 images icons for each tab bar item (1x, 2x and 3x).
First create the 3x at 75w 75h pixels (maximum: 144 x 96) and save it as iconTab0@3x.png.
Then resize it to 50w 50h pixels (maximum: 96 x 64) and save it as iconTab0@2x.png.
Finally resize it to 25w 25h pixels (maximum: 48 x 32) and save it as iconTab0.png.
Now all you need is to select those 3 images at your finder and drag them to your image assets.
Human Interface Guidelines
UITabBarItem Change Image Height and Width
from apple documentation the maximum size of a picture on the TabBar are 30x30 (60x60 for the retina display).
I do not think it's possible to take the entire height of the TabBar without strecth the image. I think the best solution is to center the image in the TabBar using imageInset
tabBarItem1.imageInsets = UIEdgeInsetsMake(6, 0, -6, 0);
otherwise you can play with this imageInset and strecth the image like in the screenshot
tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;
UITabBar *tabBar = tabBarController.tabBar;
UITabBarItem *tabBarItem1 = [tabBar.items objectAtIndex:0];
UITabBarItem *tabBarItem2 = [tabBar.items objectAtIndex:1];
UITabBarItem *tabBarItem3 = [tabBar.items objectAtIndex:2];
//add image to tabbarItems
tabBarItem1.imageInsets = UIEdgeInsetsMake(0, -10, -6, -10);
}
What size should TabBar images be?
30x30 is points, which means 30px @1x, 60px @2x, not somewhere in-between. Also, it's not a great idea to embed the title of the tab into the image—you're going to have pretty poor accessibility and localization results like that.
Tab Bar Icon Size
You should not do this yourself system can do it automatically.
here is Apple Human Interface Guidelines, where you can find icon resolutions:
https://developer.apple.com/ios/human-interface-guidelines/icons-and-images/custom-icons/
for example:
If you are using circular icons for tab bar item you should create following sizes for portrait mode:
- 75px × 75px (25pt × 25pt @3x)
- 50px × 50px (25pt × 25pt @2x)
- 25px × 25px (25pt × 25pt @1x)
and for landscape mode:
- 54px × 54px (18pt × 18pt @3x)
- 36px × 36px (18pt × 18pt @2x)
- 18px × 18px (18pt × 18pt @1x)
after you add this icons into Assets.xcassets or somewhere, you can select Tab Bar Item icons from storyboard:
select Tab Bar item and in the attributes inspector, choose portrait image for image field, and landscape image for landscape field.
After that system will do everything for you.
What are the correct dimensions for a custom tab bar item background image?
Making my answer for this question how to make UITabBar selection indicator image fill the whole space? as reference:
Subclass the UITabBarController
It works for multiple devices even with rotation.
Notes:
- Make your images' rendering mode as
Original
. Assign this class below to your UITabBarController in your Storyboard or as your base class if you're doing your screen programmatically.
//
// BaseTabBarController.swift
// MyApp
//
// Created by DRC on 1/27/17.
// Copyright © 2017 PrettyITGirl. All rights reserved.
//
import UIKit
class BaseTabBarController: UITabBarController {
let numberOfTabs: CGFloat = 4
let tabBarHeight: CGFloat = 60
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
updateSelectionIndicatorImage()
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
updateSelectionIndicatorImage()
}
func updateSelectionIndicatorImage() {
let width = tabBar.bounds.width
var selectionImage = UIImage(named:"myimage.png")
let tabSize = CGSize(width: width/numberOfTabs, height: tabBarHeight)
UIGraphicsBeginImageContext(tabSize)
selectionImage?.draw(in: CGRect(x: 0, y: 0, width: tabSize.width, height: tabSize.height))
selectionImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
tabBar.selectionIndicatorImage = selectionImage
}
}
Maximize Size of UITabBarItem image
Regardless of the icon’s visual style, create a toolbar or navigation bar icon in the following sizes:
About 44 x 44 pixels
About 22 x 22 pixels (standard resolution)
Regardless of the icon’s visual style, create a tab bar icon in the following sizes:
About 50 x 50 pixels (96 x 64 pixels maximum)
About 25 x 25 pixels (48 x 32 pixels maximum) for standard resolution
Have a look at these Developers guide for bar & buttons
Bar icons in Human interface guidelines
Related Topics
Iphone: How to Get Current Milliseconds
Nspersistentcontainer Concurrency for Saving to Core Data
Iphone: Hide Uitableview Search Bar by Default
Best Way to Cache Images on iOS App
How to Create Managedobjectcontext Using Swift 3 in Xcode 8
How to Change Color of Single Word in Uitextview and Uitextfield
Add Bottom Border Line to UI Textfield View in Swiftui/Swift/Objective-C/Xamarin
How to Load a Xib File in a Uiview
Workaround on the Threads Limit in Grand Central Dispatch
How to Hide Keyboard in Swift on Pressing Return Key
Alternative to Dtsendsignalflag to Identify Key Events in Instruments
Uitextview That Expands to Text Using Auto Layout
How to Create a Scroll View with a Page Control Using Swift
One Step Affine Transform for Rotation Around a Point