簡體   English   中英

移除 UITabbar 上邊框線

[英]Remove UITabbar upper border line

我一直在應用程序中使用 UITabbar。 UITabbar 頂部有一條上邊界線。 參考下圖:-

我用谷歌搜索並嘗試了建議的代碼,如:-

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

[[UITabBar appearance] setShadowImage:nil];

self.navigationController.toolbar.clipsToBounds = YES;

但他們都沒有工作。 有什么解決辦法嗎?

在此處輸入圖片說明

tabBar.clipsToBounds = YES; 是為我工作。

適用於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()
        }
    } 
}
[self.tabBar setValue:@(YES) forKeyPath:@"_hidesShadow"];

或者你可以使用

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

或者

 [[UITabBar appearance] setShadowImage:nil];

Swift 5 對我有用

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


    }

這對我適用於 iOS 11,XCode 9.4

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

改進上面的一個答案 - 仍然有點黑客,但效果更好。 上面的答案將隱藏帶有自定義圖像的 imageView。

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

您只需要添加這兩行代碼即可從 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;
}

前:

在此處輸入圖片說明

后:

在此處輸入圖片說明

更新:您也可以設置背景圖像並將陰影設置為 nil,如下面的代碼

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

輸出:

在此處輸入圖片說明

shadowImage財產UITabbar負責對這個邊界線(灰色陰影) UITabbar 更新此屬性的值以將其刪除。

試試這個,** Objective-C **

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

// or 

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

** 斯威夫特 **

//Remove shadow image by assigning nil value.
UITabBar.appearance().shadowImage = nil

// or 

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


這是shadowImage 的蘋果指南

@available(iOS 6.0, *)
open var shadowImage: UIImage?

默認為零。 當非零時,顯示自定義陰影圖像而不是默認陰影圖像。 要顯示自定義陰影,還必須使用 -setBackgroundImage: 設置自定義背景圖像(如果使用默認背景圖像,則將使用默認陰影圖像)。

如果不限制到邊界,我就找不到答案。 而使用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()
    }
}

更新您的控制器方法

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

self.navigationController?.setNavigationBarHidden(真,動畫:真)

為我工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM