Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't set tab bar shadow image in iOS 13

Before iOS13, I used the code below to remove the tab bar top border:

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()

But it does not work with iOS13, and I am looking for a solution to this. Do you have any thoughts?

like image 458
Volodymyr Davydenko Avatar asked Sep 23 '19 12:09

Volodymyr Davydenko


2 Answers

Swift 4+:

In your TabBarController class write this:

 if #available(iOS 13, *) {
        let appearance = self.tabBar.standardAppearance.copy()
        appearance.backgroundImage = UIImage()
        appearance.shadowImage = UIImage()
        appearance.shadowColor = .clear
        self.tabBar.standardAppearance = appearance
    } else {
        self.tabBar.shadowImage = UIImage()
        self.tabBar.backgroundImage = UIImage()
    }

For title adjustment use this:

appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -12)

For Objective C

  if (@available(iOS 13.0, *)) {
    UITabBarAppearance* appearance =  self.tabBar.standardAppearance.copy;
    appearance.backgroundImage = [UIImage new];
    appearance.shadowImage = [UIImage new];
    appearance.shadowColor = [UIColor clearColor];
    // Title adjustment
    appearance.stackedLayoutAppearance.normal.titlePositionAdjustment = UIOffsetMake(0, -12);
    self.tabBar.standardAppearance = appearance;
} else {
    self.tabBar.shadowImage = [UIImage new];
    self.tabBar.backgroundImage = [UIImage new];
}
like image 177
Sargis Terteryan Avatar answered Oct 11 '22 11:10

Sargis Terteryan


In iOS 13 you can use an appearance based approach with built in methods for configuring transparency:

    if #available(iOS 13, *) {
        let appearance = self.tabBar.standardAppearance.copy()
        appearance.configureWithTransparentBackground()
        tabBar.standardAppearance = appearance
    } else {
        tabBar.backgroundImage = UIImage()
        tabBar.shadowImage = UIImage()
        tabBar.barTintColor = UIColor.clear
    }

And to change it back again, you can do the same using configureWithDefaultBackground():

    if #available(iOS 13, *) {
        let appearance = self.tabBar.standardAppearance.copy()
        appearance.configureWithDefaultBackground()
        tabBar.standardAppearance = appearance
    } else {
        tabBar.barTintColor = nil
        tabBar.backgroundImage = nil
        tabBar.shadowImage = nil
    }
like image 41
Sune Avatar answered Oct 11 '22 11:10

Sune