iOS12.1 使用 UINavigationController + UITabBarController( UITabBar 磨砂),设置hidesBottomBarWhenPushed后,在 pop 后,会引起TabBar布局异常
如果使用系统OS12.1 UINavigationController + UITabBarController( UITabBar 磨砂),在popViewControllerAnimated 会遇到tabbar布局错乱的问题:
其中触发该问题的代码如下:
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated{
if (self.childViewControllers.count > 0) {
//如果没这行代码,是正常显示的
viewController.hidesBottomBarWhenPushed = YES;
}
[super pushViewController:viewController animated:animated];
}
经过 @YaoJuan @MoLice 的提示,可以使用 QMUI_iOS/issues 提到的解决方案解决:
这个问题是 iOS 12.1 Beta 2 引入的问题,只要 UITabBar 是磨砂的,并且 push viewController 时 hidesBottomBarWhenPushed = YES 则手势返回的时候就会触发。
出现这个现象的直接原因是 tabBar 内的按钮 UITabBarButton 被设置了错误的 frame,frame.size 变为 (0, 0) 导致的。如果12.1正式版Apple修复了这个bug可以移除调这段代码(来源于QMUIKit的处理方式),如果12.1正式版本Apple Fix了这个bug,可以移除掉这个bug
在这里有讨论: https://github.com/ChenYilong/CYLTabBarController/issues/312
具体的解决方案是:
需要进行如下设置:
// .h
@interface CYLTabBar : UITabBar
@end
// .m
#import "CYLTabBar.h"
/**
* 用 block 重写某个 class 的指定方法
* @param targetClass 要重写的 class
* @param targetSelector 要重写的 class 里的实例方法,注意如果该方法不存在于 targetClass 里,则什么都不做
* @param implementationBlock 该 block 必须返回一个 block,返回的 block 将被当成 targetSelector 的新实现,所以要在内部自己处理对 super 的调用,以及对当前调用方法的 self 的 class 的保护判断(因为如果 targetClass 的 targetSelector 是继承自父类的,targetClass 内部并没有重写这个方法,则我们这个函数最终重写的其实是父类的 targetSelector,所以会产生预期之外的 class 的影响,例如 targetClass 传进来 UIButton.class,则最终可能会影响到 UIView.class),implementationBlock 的参数里第一个为你要修改的 class,也即等同于 targetClass,第二个参数为你要修改的 selector,也即等同于 targetSelector,第三个参数是 targetSelector 原本的实现,由于 IMP 可以直接当成 C 函数调用,所以可利用它来实现“调用 super”的效果,但由于 targetSelector 的参数个数、参数类型、返回值类型,都会影响 IMP 的调用写法,所以这个调用只能由业务自己写。
*/
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 {
/* 这个问题是 iOS 12.1 Beta 2 的问题,只要 UITabBar 是磨砂的,并且 push viewController 时 hidesBottomBarWhenPushed = YES 则手势返回的时候就会触发。
出现这个现象的直接原因是 tabBar 内的按钮 UITabBarButton 被设置了错误的 frame,frame.size 变为 (0, 0) 导致的。如果12.1正式版Apple修复了这个bug可以移除调这段代码(来源于QMUIKit的处理方式)*/
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]) {
// 如果发现即将要设置一个 size 为空的 frame,则屏蔽掉本次设置
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
目前我已经在我写的这个库 CYLTabBarController 进行了修改兼容。
相关代码修改:https://github.com/ChenYilong/CYLTabBarController/commit/2c741c8bffd47763ad2fca198202946a2a63c4fc
更多change log见:https://github.com/ChenYilong/CYLTabBarController/releases
你好,该解决方案及其所使用的代码,来自于 QMUI iOS,若需使用,请在你的代码里声明来源。
该解决方案来源:https://github.com/QMUI/QMUI_iOS/issues/410#issuecomment-432574291
多些@MoLice 提醒,相关代码是有使用者提交的PR https://github.com/ChenYilong/CYLTabBarController/pull/317 ,在使用时未了解到出处故未添加注释,现在已经在代码中添加来源。https://github.com/ChenYilong/CYLTabBarController/commit/353f990e7785e2267a4c0a3e4ab94dddb218b9f0
这个方法在IphoneX上面运行 还是会跳动的
[UITabBar appearance].translucent = NO;
@ChenYilong 大佬,如果没用你这个库,用的系统的,怎么处理
没用这个库、处理方法一样
原生的Tabbar用上面方法好像无效;同时侧滑返回时,即将展现的VC主题颜色会变灰,等完全呈现之后就恢复了白色的背景; 模拟器 + Xcode 10.1 + iOS 12.1
兼容iPhoneX的话可以在Block里面添加下面代码:
static CGFloat const kIPhoneXTabbarButtonErrorHeight = 33;
static CGFloat const kIPhoneXTabbarButtonHeight = 48;
return ^(UIView *selfObject, CGRect firstArgv) {
if ([selfObject isKindOfClass:originClass]) {
// 如果发现即将要设置一个 size 为空的 frame,则屏蔽掉本次设置
if (!CGRectIsEmpty(selfObject.frame) && CGRectIsEmpty(firstArgv)) {
return;
}
}
//兼容IphoneX
if (firstArgv.size.height == kIPhoneXTabbarButtonErrorHeight) {
firstArgv.size.height = kIPhoneXTabbarButtonHeight;
}
// call super
void (*originSelectorIMP)(id, SEL, CGRect);
originSelectorIMP = (void (*)(id, SEL, CGRect))originIMP;
originSelectorIMP(selfObject, originCMD, firstArgv);
};
@lilingyu0620 高度 33 可能是另一个系统 bug,可以参考 https://github.com/QMUI/QMUI_iOS/issues/422。
以及,你还需要考虑横屏下的错误值 20。
另外,对那个 block 的修改代码应该都放到 if ([selfObject isKindOfClass:originClass]) 内。
@MoLice nice...
一个小问题哈,如果想用 swift 要怎么实现呢, 我简单的写了一下,但没成功
class FixedTabBar: UITabBar {
typealias NewIMPBlock = (UIView, CGRect)->Void
public static func swizzleFrameSetterImplementation() {
let selfClass: AnyClass! = NSClassFromString("UITabBarButton")
let sel: Selector = #selector(setter: frame)
if let originMethod: Method = class_getInstanceMethod(selfClass, sel) {
let impBlock: (AnyClass, Selector, IMP) -> NewIMPBlock = {originClass, originMethod, originIMP in
return { (selfObject, frameArgv) in
if !selfObject.frame.isEmpty, frameArgv.isEmpty {
return
}
//shoudl call super here .....
return
}
}
let newImp = imp_implementationWithBlock(impBlock)
method_setImplementation(originMethod, newImp)
}
}
}
extension UIApplication {
override open var next: UIResponder? {
// FixedTabBar.swizzleFrameSetterImplementation()
return super.next
}
}
在这里有讨论: v1.17.21 已经发布:
change log:
- [work-arounds]compatible for iPhoneX for issue #312
- work-arounds 方案,解决 iOS12.1上,iPhoneX 位置跳动的问题。
相关提交见:https://github.com/ChenYilong/CYLTabBarController/commit/2ec965962b70b3c282406265b572bf10af8bb648
一个小问题哈,如果想用 swift 要怎么实现呢, 我简单的写了一下,但没成功
class FixedTabBar: UITabBar { typealias NewIMPBlock = (UIView, CGRect)->Void public static func swizzleFrameSetterImplementation() { let selfClass: AnyClass! = NSClassFromString("UITabBarButton") let sel: Selector = #selector(setter: frame) if let originMethod: Method = class_getInstanceMethod(selfClass, sel) { let impBlock: (AnyClass, Selector, IMP) -> NewIMPBlock = {originClass, originMethod, originIMP in return { (selfObject, frameArgv) in if !selfObject.frame.isEmpty, frameArgv.isEmpty { return } //shoudl call super here ..... return } } let newImp = imp_implementationWithBlock(impBlock) method_setImplementation(originMethod, newImp) } } } extension UIApplication { override open var next: UIResponder? { // FixedTabBar.swizzleFrameSetterImplementation() return super.next } }
你好,这里有一个解决方案是用 swift写的,你可以参照一下。我试过了,可以用。 https://github.com/tonySwiftDev/UITabbar-fixIOS12.1Bug
sounds good, @tonySwiftDev , I'll try your solution
sounds good, @tonySwiftDev , I'll try your solution
thank you very much .I make some change just now. pls download the new file .
这个方法在IphoneX上面运行 还是会跳动的
自定义一个UITabbar:
@property (nonatomic) UIEdgeInsets oldSafeAreaInsets;
// 解决iPhonX上push时UITabBar上移的问题
- (void) safeAreaInsetsDidChange{
[super safeAreaInsetsDidChange];
if(self.oldSafeAreaInsets.left != self.safeAreaInsets.left ||
self.oldSafeAreaInsets.right != self.safeAreaInsets.right ||
self.oldSafeAreaInsets.top != self.safeAreaInsets.top ||
self.oldSafeAreaInsets.bottom != self.safeAreaInsets.bottom)
{
self.oldSafeAreaInsets = self.safeAreaInsets;
[self invalidateIntrinsicContentSize];
[self.superview setNeedsLayout];
[self.superview layoutSubviews];
}
}
- (CGSize) sizeThatFits:(CGSize) size{
CGSize fixSize = [super sizeThatFits:size];
if(@available(iOS 11.0, *))
{
CGFloat bottomInset = self.safeAreaInsets.bottom;
if( bottomInset > 0 && fixSize.height < 50) {
fixSize.height += bottomInset;
}
}
return fixSize;
}