Hello everyone, I'm new to this forum. I've been trying to customize my Tab Bar Controller for over 2 hours now.
Since I want to use more than 5 icons, specifically 6 icons, I had to change my code from:
Method 1:
class BaseTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
self.selectedIndex = 2
}
override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
// Zugriff auf die Tab Bar und ihre Position anpassen
var newFrame = self.tabBar.frame
newFrame.origin.y += 22 // Verschiebt die Tab Bar 10 Punkte nach unten
self.tabBar.frame = newFrame
}
}
to
Method 2:
class BaseTabBarController: UITabBarController {
override func viewDidLoad() {
super.viewDidLoad()
// Setze eine benutzerdefinierte TraitCollection, um mehr Items anzuzeigen
self.tabBarController?.tabBar.isTranslucent = false
self.tabBarController?.tabBar.items?.forEach { item in
// Optional: Anpassung der TabBarItems
item.imageInsets = UIEdgeInsets(top: 0, left: -5, bottom: 0, right: -5)
item.titlePositionAdjustment = UIOffset(horizontal: -10, vertical: 0)
}
}
// Verwende diese Methode, um den Tab-Wechsel ohne Animation durchzuführen
func changeTabWithoutAnimation(to index: Int) {
// Deaktivieren der Animationen
UIView.setAnimationsEnabled(false)
self.selectedIndex = index // Setze den Tab ohne Animation
UIView.setAnimationsEnabled(true) // Animationen wieder aktivieren
}
// Anpassung der TabBar-Items für mehr Tabs
override var traitCollection: UITraitCollection {
let realTraits = super.traitCollection
let fakeTraits = UITraitCollection(horizontalSizeClass: .regular)
return UITraitCollection(traitsFrom: [realTraits, fakeTraits])
}
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
// Setze die Höhe der TabBar explizit auf 20px und verschiebe sie nach unten
var tabBarFrame = self.tabBar.frame
tabBarFrame.size.height = 20
tabBarFrame.origin.y = self.view.frame.size.height - 20 // Verschiebe die TabBar nach unten
self.tabBar.frame = tabBarFrame
// Verteile die TabBarItems gleichmäßig auf der TabBar, falls mehr als 5 vorhanden sind
if let items = self.tabBar.items, items.count > 5 {
let totalWidth = self.view.frame.size.width
let itemWidth = totalWidth / CGFloat(items.count)
// Verhindere, dass Items nach rechts verschoben werden
self.tabBar.frame.size.width = totalWidth
for item in items {
item.imageInsets = UIEdgeInsets(top: 0, left: -itemWidth / 2, bottom: 0, right: -itemWidth / 2)
}
}
// Kürze nur den Titel des 6. TabItems, wenn der Platz nicht ausreicht
if let items = self.tabBar.items, items.count >= 6 {
let lastItem = items[5] // Das 6. Tab ist Index 5
if let title = lastItem.title, title.count > 5 {
let truncatedTitle = String(title.prefix(10)) + "..."
lastItem.title = truncatedTitle
}
}
}
}
Now, in Code 2, I have the issue that I can no longer adjust the height of the Tab Bar to move it down by 20 pixels as I did in Code 1. Additionally, the tabs are being animated again instead of switching to Tab 2 without animation. I’m really frustrated at this point and would be very grateful for any help! Thanks.
Kind Regards