删除UITabbar上边框线

时间:2015-09-18 06:54:38

标签: ios border line uitabbar

我一直在应用中使用UITabbar。 UITabbar顶部有一条上边界线。 请参阅下图: -

我用Google搜索并尝试了建议的代码,如: -

[[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];

另外

[[UITabBar appearance] setShadowImage:nil];

self.navigationController.toolbar.clipsToBounds = YES;

但他们都没有工作。任何解决方案?

enter image description here

11 个答案:

答案 0 :(得分:10)

tabBar.clipsToBounds = YES;对我有用。

答案 1 :(得分:8)

[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];

或者您可以使用

[[UITabBar appearance] setShadowImage:[UIImage imageNamed:@"transparentShadow.png"]];

 [[UITabBar appearance] setShadowImage:nil];

答案 2 :(得分:3)

使用 iOS 13和Swift 5 的工作解决方案:

/** 
 * A custom subclass of `UITabBarController` to use whenever you want 
 * to hide the upper border of the `UITabBar`.
 */
class TabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        tabBar.backgroundColor = UIColor.white

        // Removing the upper border of the UITabBar.
        // 
        // Note: Don't use `tabBar.clipsToBounds = true` if you want 
        // to add a custom shadow to the `tabBar`!
        // 
        if #available(iOS 13, *) {
            // iOS 13:
            let appearance = tabBar.standardAppearance
            appearance.configureWithOpaqueBackground()
            appearance.shadowImage = nil
            appearance.shadowColor = nil
            tabBar.standardAppearance = appearance
        } else {
            // iOS 12 and below:
            tabBar.shadowImage = UIImage()
            tabBar.backgroundImage = UIImage()
        }
    } 
}

答案 3 :(得分:2)

这适用于iOS 11,XCode 9.4

UITabBar.appearance().shadowImage = UIImage()
UITabBar.appearance().backgroundImage = UIImage()
UITabBar.appearance().backgroundColor = UIColor.white

答案 4 :(得分:2)

对以上答案之一进行改进-仍然有点破解,但效果更好。上面的答案将隐藏具有自定义图像的imageView。

    for tabBarSubview in self.tabBar.subviews {
        let tabBarSubviewName = String(describing: type(of: tabBarSubview))
        guard tabBarSubviewName == "_UIBarBackground" else { continue }
        tabBarSubview.clipsToBounds = true
    }

答案 5 :(得分:1)

您只需添加以下两行代码即可从UITabbar中删除边框:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {


    [[UITabBar appearance] setBackgroundImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    // Override point for customization after application launch.
    return YES;
}

在:

enter image description here

后:

enter image description here

<强>更新 您也可以设置背景图像并将阴影设置为nil,如下面的代码

    UIImage* tabBarBackground = [UIImage imageNamed:@"tabbar.png"];
    [[UITabBar appearance] setShadowImage:[[UIImage alloc] init]];
    [[UITabBar appearance] setBackgroundImage:tabBarBackground];

输出:

enter image description here

答案 6 :(得分:1)

shadowImage的{​​{1}}属性负责UITabbar上的此边框线(灰色阴影)。更新此属性的值以将其删除。

试试这个, **目标-C **

UITabbar

** Swift **

//Remove shadow image by assigning nil value.
[[UITabBar appearance] setShadowImage: nil];

// or 

// Assing UIImage instance without image reference
[[UITabBar appearance] setShadowImage: [[UIImage alloc] init]];


这是shadowImage

的苹果指南
//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil

// or 

// Assing UIImage instance without image reference
UITabBar.appearance().shadowImage = UIImage()
  

默认为零。当非零时,显示自定义阴影图像而不是   默认阴影图像。要显示自定义阴影,请自定义   必须使用-setBackgroundImage设置背景图像:(如果是   使用默认背景图像,默认阴影图像将是   使用)。

答案 7 :(得分:1)

Swift 5对我有用

override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        myTabBar.clipsToBounds = true


    }

答案 8 :(得分:0)

我找不到没有限制的答案。使用UITabBar.appearance().shadowImage的方式 似乎已经过时了

所以我做了一个不完美的,但是工作和安全的解决方案是为iOS10和iOS11 删除UITabBar阴影而没有剪切到界限。也适用于iPhone X(如果没有,会很奇怪;))

以下代码贯穿UITabBarController层次结构并查找阴影视图。哪个(现在)是UIImageView

的子视图_UIBarBackground
extension UITabBarController {
    public func hideTopShadow() {
        // looking for tabBar
        for subview in self.view.subviews {
            let tabBarSubviewName = String(describing: type(of: subview))
            guard tabBarSubviewName == "UITabBar" else { continue }

            // looking for _UIBarBackground. The other subivews are UITabBarButtons
            for tabBarSubview in subview.subviews {
                let tabBarSubviewName = String(describing: type(of: tabBarSubview))
                guard tabBarSubviewName == "_UIBarBackground" else { continue }

                // looking for UIImageView. This is the only subview
                for shadowView in tabBarSubview.subviews where shadowView is UIImageView {
                    shadowView.isHidden = true
                    return
                }
            }
        }
        print(" **** ERROR: Could not find the shadow view \(self.self) \(#function)")
    }
}

可能的用法。我有一个UITabBarController的子类,所以我做了以下:

// to avoid excessive runs through the hierarchy after the shadow was hidden
fileprivate var hasHiddenShadow: Bool = false

override open func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    guard !hasHiddenShadow else { return }

    hasHiddenShadow = true
    DispatchQueue.main.asyncAfter(deadline: .now()) {
        self.hideTopShadow()
    }
}

答案 9 :(得分:0)

更新您的控制器方法

override func viewDidLoad() {
    super.viewDidLoad()
    self.tabBar.clipsToBounds = true
    if #available(iOS 13, *) {
        self.tabBar.standardAppearance.shadowImage = nil
        self.tabBar.standardAppearance.shadowColor = nil
    }
}

答案 10 :(得分:0)

self.navigationController?.setNavigationBarHidden(true, animation: true)

为我工作