UITabBarItem - Position image separately from title text - ios

I have a UITabBar with 5 UITabBarItems, each containing an image and a title. Currently, the images are positioned centered above the title text. When an item is selected I bold the text, however this causes the positioning of the image to change slightly since the text now takes up more space. How can I divorce the positioning of these two elements in a UITabBarItem?

Try adjusting the image insets to get your desired look.
For example, you could try:
tabBarItem.imageInsets = UIEdgeInsets(top: -1, left: 0, bottom: 1, right: 0)

Related

How to set the left inset of UICollectionViewListCell text to 0

I created a list by using collection view compositional layout and implemented a default style cell ( please see the picture UICollectionViewListCell
There is a space of 20pt on the left side of the text. The space might be different on different screen sizes.
I tried to set its leading inset to 0, but the space won't reflect the value unless the value is bigger than 20
section.contentInsets = .init(top: 0, leading: 0, bottom: 0, trailing: 0)
Is there any way to remove the space? or how can I get the space value?
UICollectionViewListCell has a property called indentationWidth which is used to configure the indentation width. You might also want to look at indentationLevel property as well.
let cellRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, Item> { cell, indexPath, item in
cell.indentationWidth = 0
}

How to vertically center a font glyph?

I have a UIButton with title "⟳". The title does not appear to be vertically centered due to the position of "⟳".
I understand that offsets of the UIButton can be changed to solve the problem in this instance.
Can anyone point out if there is any other way to vertically center the "⟳" glyph?
In this image, all 4 green buttons are using 64-pt System font, with the .titleLabel background set to yellow:
As you can see with the top button, an upper-case "O" is centered vertically and horizontally.
The "⟳" unicode symbol you are using is more like a lower-case character - as seen in the 2nd button.
By itself, in the 3rd button, we see it appears "shifted" down and to the left.
The symbol in the bottom button is center-aligned by adjusting the Title Edge Insets:
.titleEdgeInsets = UIEdgeInsets(top: 0, left: 8, bottom: 14, right: 0)

How to center a SF Symbols image vertically in UITabBarItem?

I am using SF Symbols images as tab images in my iOS app by assigning them as follows:
self.tabBarItem.image = UIImage(systemName: "ellipsis")
This results in all images being top-aligned, but I would like to have them centered vertically.
What do I have to do for this?
Apparently SF symbols are rendered with system font size by default. So if you add a baseline offset of half that size to the ellipsis symbol you could almost perfectly center it vertically that way.
It's only almost perfect because ellipsis symbol has a height of its own which is not accounted for by this solution, even if it is not much.
self.tabBarItem.image = UIImage(systemName: "ellipsis")!.withBaselineOffset(fromBottom: UIFont.systemFontSize / 2)
The best solution, which will make your button identical to "tabBarSystemItem: .more", but with the possibility of removing the title and applying Configuration is the following:
self.tabBarItem.image = UIImage(systemName: "ellipsis", withConfiguration:
UIImage.SymbolConfiguration(weight: .black))!.imageWithoutBaseline()
You have to set the image insets on the UITabBarItem to shift your icons down 6 or 7px (depending on your icon size).
In code:
I've done this by creating a subclass of UITabBarController and adding this code to the bottom of my viewDidLoad method:
tabBar.items?.forEach({
$0.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
})
(You could also do this in your ViewController classes, if that's where you configure each of their tab bar items.)
In storyboards:
You can also do it using storyboards by selecting your TabBarItem in the storyboard and adjusting the insets in the info panel:

TextView And Its Content

I want to place information text in main scene of the application. When ı place a text view, its content starts at the middle. There is a between the text and top boundary of the TextView.
How can I make the text start at the first line of the TextView ?
You can set the contentOffset property of textView to zero like
textView.setContentOffset(CGPoint.zero, animated: false)
Swift 3
textView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

Maximize Size of UITabBarItem image

I would like to create a custom UITabBarItem with a Icon-image thats size is a little bit bigger than usual. The thing is I don't want to use a full replace of the background image because i would like to have the translucent effect of the TabBar.
So i would like to know 2 things:
What sizes are now correct for the new iOS7 UITabBarItems and their icons
How do I modify the size of the icon to display a bigger icon, because i dont want to show a title. Without the title its kinda small. In mind to keep the translucent effect displaying.
Any help or suggestions would be great!
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
To increase the size, try below code,
NSArray *items = self.tabBarController.tabBar.items;
for (UITabBarItem *b in items)
b.imageInsets = UIEdgeInsetsMake(-5, -5, -5, -5);
If you want to decrease, try passing positive values to UIEdgeInsetsMake(top,left,bottom,right)
Swift 4.2,
let array = tabBarController?.tabBar.items
for controller in array! {
controller.tabBarItem.imageInsets = UIEdgeInsets(top: 5, left: -5, bottom: -5, right: -5)
}

Resources