iOS 使用IQKeyboardManager出现导航栏电池条变白、变透明问题处理

前段时间公司App做了一个类似评论的功能,在界面底部有一个输入评论内容的UITextView控件,键盘在多次收起、弹出操作时,偶尔会在收起后出现导航栏电池条变白的问题,刚开始知道原因可能是IQKeyboardManager导致,但是一直没找到根源,一番自摸之后写了下面的方法进行处理,但是需要在每个可能出现这种问题的界面都进行处理。

- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSArray<UIView *> *naviBarSubviews = [XXXApp currentNavController].navigationBar.subviews;
    for (UIView *subView in naviBarSubviews) {
        if ([subView isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
            if (ScreenSize_X == [XXXContextInfo deviceScreenSize]) {
                subView.frame = CGRectMake(0, -44, kXXScreenWidth, 88);
            } else {
                subView.frame = CGRectMake(0, -20, kXXScreenWidth, 64);
            }
        }
    }
}

在这样处理后,电池条变回正常,但是push和pop还可能导致其他界面出现问题,又在导航控制器代理方法里做了处理。

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
    NSArray<UIView *> *naviBarSubviews = self.navigationBar.subviews;
    for (UIView *subView in naviBarSubviews) {
        if ([subView isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
            if (ScreenSize_X == [XXContextInfo deviceScreenSize]) {
                subView.frame = CGRectMake(0, -44, kXXScreenWidth, 88);
            } else {
                subView.frame = CGRectMake(0, -20, kXXScreenWidth, 64);
            }
        }
    }
}

这样处理十分麻烦,还很low,最后看到其他同学分享的处理方法,问题才得到彻底处理。方法就是找到IQKeyboardManager框架中IQUIView+Hierarchy.m文件,修改 -(UIViewController *)topMostController 方法中的代码代码如下。

- (UIViewController *)topMostController
{
    UIViewController  *rootController = [UIApplication  sharedApplication].keyWindow.rootViewController;
    if ([rootController isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarController = (UITabBarController *)rootController;
        UINavigationController *navController = tabBarController.selectedViewController;
        UIViewController *viewController = (UIViewController *)navController.visibleViewController;
        while (viewController.presentedViewController) {
            viewController = (UIViewController *)viewController.presentedViewController;
        }
        return viewController;
    } else if ([rootController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navController = (UINavigationController *)rootController;
        return navController.visibleViewController;
    } else if ([rootController isKindOfClass:[UIViewController class]]) {
        return rootController;
    } else {
        return nil;
    }
}

参考链接 IQKeyboardManager 在iOS11下引起电池栏透明

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
小程序首页导航栏颜色渐可以通过以下步骤实现: 1. 在 app.json 文件中设置 navigationBarBackgroundColor 字段为渐色,如下所示: ```json { "navigationBarBackgroundColor": { "startColor": "#FF5B5B", "endColor": "#FFC371", "gradientDirection": "vertical" } } ``` 2. 在页面的 onShow 生命周期函数中获取导航栏组件,并设置背景颜色为渐色,如下所示: ```js onShow: function () { const systemInfo = wx.getSystemInfoSync(); const { statusBarHeight, system } = systemInfo; const navBarHeight = system.indexOf('iOS') > -1 ? 44 : 48; const navBar = this.selectComponent('#nav-bar'); navBar.setNavigationBarStyle({ backgroundColor: { startColor: '#FF5B5B', endColor: '#FFC371', gradientDirection: 'vertical' }, height: navBarHeight + statusBarHeight }); } ``` 3. 在导航栏组件中添加 setNavigationBarStyle 方法,用于设置导航栏样式,如下所示: ```js Component({ methods: { setNavigationBarStyle(options) { const { backgroundColor, height } = options; const { startColor, endColor, gradientDirection } = backgroundColor; const linearGradient = `linear-gradient(${gradientDirection}, ${startColor}, ${endColor})`; wx.setNavigationBarColor({ frontColor: '#ffffff', backgroundColor: 'transparent', animation: { duration: 0, timingFunc: 'easeIn' } }); wx.setTopBarText({ text: '' }); wx.setNavigationBarStyle({ backgroundColor: 'transparent', height, success: () => { wx.createSelectorQuery().select('.custom-nav-bar').boundingClientRect(rect => { const { top } = rect; wx.createSelectorQuery().selectViewport().scrollOffset(scroll => { const { scrollTop } = scroll; const opacity = Math.max(0, Math.min(1, top / (height + statusBarHeight))); const color = `rgba(255, 255, 255, ${opacity})`; const gradient = `${linearGradient}, ${color}`; wx.setNavigationBarColor({ frontColor: '#ffffff', backgroundColor: gradient, animation: { duration: 0, timingFunc: 'easeIn' } }); }).exec(); }).exec(); } }); } } }) ``` 4. 在 wxml 文件中添加导航栏组件,如下所示: ```html <custom-nav-bar id="nav-bar" title="首页"></custom-nav-bar> ``` 通过以上步骤可以实现小程序首页导航栏颜色渐

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值