UITabBar项目在iOS 12.1上跳回导航

问题描述 投票:53回答:12

我在主屏幕上有一个带有UITabBarController的iOS应用程序,导航到隐藏UITabBarController并设置hidesBottomBarWhenPushed = true的详细屏幕。

当回到主屏幕时,UITabBarController做了一个奇怪的“跳跃”,如这个GIF所示:

enter image description here

这仅在iOS 12.1上发生,而不是在12.0或11.x上发生。

看起来像iOS 12.1的错误,因为我注意到其他应用程序如FB Messenger有这种行为,但我想知道,是否有某种解决方法呢?

ios swift uitabbar
12个回答
59
投票

在你的UITabBarController中,设置isTranslucent = false


0
投票

这是快捷的代码

extension UIApplication {
open override var next: UIResponder? {
    // Called before applicationDidFinishLaunching
    SwizzlingHelper.enableInjection()
    return super.next
}

}

class SwizzlingHelper {

static func enableInjection() {
    DispatchQueue.once(token: "com.SwizzlingInjection") {
        //what to need inject
        UITabbarButtonInjection.inject()
    }

更多信息https://github.com/tonySwiftDev/UITabbar-fixIOS12.1Bug


0
投票

我面临着完全相同的问题,即应用程序的每个选项卡都有一个导航控制器。我发现解决这个问题的最容易的非hacky方法是将UITabBarController放在UINavigationController中,并移除单个UINavigationControllers。

之前:

                   -> UINavigationController -> UIViewController
                   -> UINavigationController -> UIViewController
UITabBarController -> UINavigationController -> UIViewController
                   -> UINavigationController -> UIViewController
                   -> UINavigationController -> UIViewController

后:

                                             -> UIViewController
                                             -> UIViewController
UINavigationController -> UITabBarController -> UIViewController
                                             -> UIViewController
                                             -> UIViewController

通过使用外部UINavigationController,您无需在将视图控制器推入导航堆栈时隐藏UITabBar

警告:

到目前为止我发现的唯一问题是,在每个UIViewController上设置标题或右/左栏按钮项目的效果不同。为了克服这个问题,当可见的UITabBarControllerDelegate发生变化时,我通过UIViewController应用了这些变化。

func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
    guard let topItem = self.navigationController?.navigationBar.topItem else { return }
    precondition(self.navigationController == viewController.navigationController, "Navigation controllers do not match. The following changes might result in unexpected behaviour.")
    topItem.title = viewController.title
    topItem.titleView = viewController.navigationItem.titleView
    topItem.leftBarButtonItem = viewController.navigationItem.leftBarButtonItem
    topItem.rightBarButtonItem = viewController.navigationItem.rightBarButtonItem
}

请注意,我已经添加了一个preconditionFailure来捕捉导航架构修改后的任何情况


-1
投票

如果你仍然希望保持标签栏半透明,你需要从UITabBar子类并覆盖属性safeAreaInsets

class MyTabBar: UITabBar {

private var safeInsets = UIEdgeInsets.zero

@available(iOS 11.0, *)
override var safeAreaInsets: UIEdgeInsets {
    set {
        if newValue != UIEdgeInsets.zero {
            safeInsets = newValue
        }
    }
    get {
        return safeInsets
    }
} 

}

这个想法是不允许系统设置zero插件,因此标签栏不会跳转。


9
投票

Apple已经在iOS 12.1.1中修复了这个问题


5
投票

我想这是Apple的错误但是你可以尝试这个作为一个热门修复:只需为你的tabBar创建一个类,其代码如下:

import UIKit

class FixedTabBar: UITabBar {

    var itemFrames = [CGRect]()
    var tabBarItems = [UIView]()


    override func layoutSubviews() {
        super.layoutSubviews()

        if itemFrames.isEmpty, let UITabBarButtonClass = NSClassFromString("UITabBarButton") as? NSObject.Type {
            tabBarItems = subviews.filter({$0.isKind(of: UITabBarButtonClass)})
            tabBarItems.forEach({itemFrames.append($0.frame)})
        }

        if !itemFrames.isEmpty, !tabBarItems.isEmpty, itemFrames.count == items?.count {
            tabBarItems.enumerated().forEach({$0.element.frame = itemFrames[$0.offset]})
        }
    }
}

3
投票

这是一个可以处理旋转和添加或删除标签栏项目的解决方案:

class FixedTabBar: UITabBar {

    var buttonFrames: [CGRect] = []
    var size: CGSize = .zero

    override func layoutSubviews() {
        super.layoutSubviews()

        if UIDevice.current.systemVersion >= "12.1" {
            let buttons = subviews.filter {
                String(describing: type(of: $0)).hasSuffix("Button")
            }
            if buttonFrames.count == buttons.count, size == bounds.size {
                zip(buttons, buttonFrames).forEach { $0.0.frame = $0.1 }
            } else {
                buttonFrames = buttons.map { $0.frame }
                size = bounds.size
            }
        }
    }
}

3
投票

在我的情况下(iOS 12.1.4),我发现这种奇怪的毛刺行为是由与.modalPresentationStyle = .fullScreen一起呈现的模态触发的

在将他们的presentationStyle更新为.overFullScreen后,故障就消失了。


2
投票
import UIKit

extension UITabBar{

open override func layoutSubviews() {
    super.layoutSubviews()
    if let UITabBarButtonClass = NSClassFromString("UITabBarButton") as? NSObject.Type{
        let subItems = self.subviews.filter({return $0.isKind(of: UITabBarButtonClass)})
        if subItems.count > 0{
            let tmpWidth = UIScreen.main.bounds.width / CGFloat(subItems.count)
            for (index,item) in subItems.enumerated(){
                item.frame = CGRect(x: CGFloat(index) * tmpWidth, y: 0, width: tmpWidth, height: item.bounds.height)
                }
            }
        }
    }

open override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
    if let view:UITabBar = super.hitTest(point, with: event) as? UITabBar{
        for item in view.subviews{
            if point.x >= item.frame.origin.x  && point.x <= item.frame.origin.x + item.frame.size.width{
                return item
                }
            }
        }
        return super.hitTest(point, with: event)
    }
}

1
投票

有两种方法可以解决这个问题,首先,在你的UITabBarController中,设置isTranslucent = false,如:

[[UITabBar appearance] setTranslucent:NO];

其次,如果第一个解决方案没有修复您的问题,请尝试这种方式:

这是Objective-C代码

// .h
@interface CYLTabBar : UITabBar
@end 

// .m
#import "CYLTabBar.h"

CG_INLINE BOOL
OverrideImplementation(Class targetClass, SEL targetSelector, id (^implementationBlock)(Class originClass, SEL originCMD, IMP originIMP)) {
   Method originMethod = class_getInstanceMethod(targetClass, targetSelector);
   if (!originMethod) {
       return NO;
   }
   IMP originIMP = method_getImplementation(originMethod);
   method_setImplementation(originMethod, imp_implementationWithBlock(implementationBlock(targetClass, targetSelector, originIMP)));
   return YES;
}
@implementation CYLTabBar

+ (void)load {

   static dispatch_once_t onceToken;
   dispatch_once(&onceToken, ^{
       if (@available(iOS 12.1, *)) {
           OverrideImplementation(NSClassFromString(@"UITabBarButton"), @selector(setFrame:), ^id(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
               return ^(UIView *selfObject, CGRect firstArgv) {

                   if ([selfObject isKindOfClass:originClass]) {

                       if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
                           return;
                       }
                   }

                   // call super
                   void (*originSelectorIMP)(id, SEL, CGRect);
                   originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
                   originSelectorIMP(selfObject, originCMD, firstArgv);
               };
           });
       }
   });
}
@end

更多信息:https://github.com/ChenYilong/CYLTabBarController/commit/2c741c8bffd47763ad2fca198202946a2a63c4fc


1
投票

您可以使用以下方法覆盖几个iOS 12颠倒的- (UIEdgeInsets)safeAreaInsets方法:

- (UIEdgeInsets)safeAreaInsets {
    UIEdgeInsets insets = [super safeAreaInsets];
    CGFloat h = CGRectGetHeight(self.frame);
    if (insets.bottom >= h) {
        insets.bottom = [self.window safeAreaInsets].bottom;
    }
    return insets;
}

0
投票

感谢@ElonChan的想法,我只是将c内联函数更改为OC静态方法,因为我不会太多使用这个overrideImplementation。此外,此片段现已调整为iPhoneX。

static CGFloat const kIPhoneXTabbarButtonErrorHeight = 33;
static CGFloat const kIPhoneXTabbarButtonHeight = 48;


@implementation FixedTabBar


typedef void(^NewTabBarButtonFrameSetter)(UIView *, CGRect);
typedef NewTabBarButtonFrameSetter (^ImpBlock)(Class originClass, SEL originCMD, IMP originIMP);


+ (BOOL)overrideImplementationWithTargetClass:(Class)targetClass targetSelector:(SEL)targetSelector implementBlock:(ImpBlock)implementationBlock {
    Method originMethod = class_getInstanceMethod(targetClass, targetSelector);
    if (!originMethod) {
        return NO;
    }
    IMP originIMP = method_getImplementation(originMethod);
    method_setImplementation(originMethod, imp_implementationWithBlock(implementationBlock(targetClass, targetSelector, originIMP)));
    return YES;
}


+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (@available(iOS 12.1, *)) {
            [self overrideImplementationWithTargetClass:NSClassFromString(@"UITabBarButton")
                                         targetSelector:@selector(setFrame:)
                                         implementBlock:^NewTabBarButtonFrameSetter(__unsafe_unretained Class originClass, SEL originCMD, IMP originIMP) {
                return ^(UIView *selfObject, CGRect firstArgv) {
                    if ([selfObject isKindOfClass:originClass]) {
                        if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
                            return;
                        }
                        if (firstArgv.size.height == kIPhoneXTabbarButtonErrorHeight) {
                            firstArgv.size.height = kIPhoneXTabbarButtonHeight;
                        }
                    }
                    void (*originSelectorIMP)(id, SEL, CGRect);
                    originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
                    originSelectorIMP(selfObject, originCMD, firstArgv);
                };
            }];
        }
    });
}

@end
© www.soinside.com 2019 - 2024. All rights reserved.