前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >iOS小技能:下拉刷新控件的适配

iOS小技能:下拉刷新控件的适配

作者头像
公众号iOS逆向
发布2022-08-22 12:37:20
8830
发布2022-08-22 12:37:20
举报
文章被收录于专栏:iOS逆向与安全

前言

  1. 下拉顶部背景色设置: 往tableView的父控件添加拉伸背景视图
  2. present 半屏适配

iOS13 modalPresentationStyle属性默认不是全屏样式UIModalPresentationFullScreen,而是半屏样式,需要根据需求手动设置。present 半屏,会导致列表下拉刷新失效。

I 下拉刷新适配

1.1 下拉顶部背景色设置

在这里插入图片描述

  1. 设置下拉样式
代码语言:javascript
复制
#import <MJRefresh/MJRefresh.h>
@interface ERPMJRefreshNormalHeader4StyleWhite : MJRefreshNormalHeader

+ (instancetype)headerWithRefreshingTarget:(id)target refreshingAction:(SEL)action
{
    MJRefreshHeader *cmp = [[self alloc] init];
    
    [cmp setRefreshingTarget:target refreshingAction:action];
    [self setupStyleWhite:cmp];
    return cmp;
}

+(void)setupStyleWhite:(MJRefreshNormalHeader*)mj_header{
    
    
    mj_header.stateLabel.textColor = UIColor.whiteColor;
    mj_header.lastUpdatedTimeLabel.textColor=mj_header.stateLabel.textColor;
    mj_header.loadingView.activityIndicatorViewStyle =UIActivityIndicatorViewStyleWhite;
    
}

  1. 下拉顶部背景色设置:往tableView的父控件添加拉伸背景视图
代码语言:javascript
复制
        ERPMJRefreshNormalHeader4StyleWhite *mj_header =[ERPMJRefreshNormalHeader4StyleWhite headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];        
        
        UIImage *bgImg = [UIImage getMaingradientColorImage];
        
        UIColor *tmpColor = [UIColor colorWithPatternImage:bgImg];
        
        mj_header.backgroundColor =tmpColor;
        
        [self setupStretchViewColor:mj_header.backgroundColor tableView:_vcView.tableView];
        
        _vcView.tableView.mj_header = mj_header;

        _vcView.tableView.bounces = YES;

代码语言:javascript
复制

- (void)setupStretchViewColor:(UIColor*) stretchViewColor tableView:(UIView*)tableView{
    

    [tableView addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew context:nil];
    
    
    self.stretchView.backgroundColor =stretchViewColor;
    //往tableView的父控件添加拉伸背景视图
    [tableView.superview addSubview:self.stretchView];
    

}

#pragma mark - 拉顶部背景色
-(void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id>*)change context:(void*)context{
    if([keyPath isEqualToString:@"contentOffset"]){
        NSValue *value = change[NSKeyValueChangeNewKey];
        CGFloat chaneoffsetY = value.UIOffsetValue.vertical;
        UIView *mj_header= self.vcView.tableView.mj_header;
        
        self.stretchView.frame = CGRectMake(0,0,mj_header.width,-chaneoffsetY-mj_header.height);
    }
}

/**
 拉伸背景

 @return view
 */
-(UIView *)stretchView{
    
    if (!_stretchView) {
        
        UIView *tmp =[[UIView alloc] init];
        _stretchView =tmp;
//        _stretchView.backgroundColor = self.backgroundColor;
    }
    return _stretchView;
}


1.2 present 半屏适配

  1. 手动设置全屏样式
代码语言:javascript
复制
xxNavigationViewController *nav = [[xxNavigationViewController alloc] initWithRootViewController:loginViewController];
nav.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:nav animated:YES completion:nil];
//推荐使用UIModalPresentationOverFullScreen

  1. 灵活控制模态展示的视图样式

iOS13适配【灵活控制模态展示的视图样式】(全屏/下滑返回)文中提供完整demo源码

  1. 全局hook presentViewController方法
代码语言:javascript
复制
//
//  UIViewController+ERPPresent13.h

#import <UIKit/UIKit.h>
#import <objc/runtime.h>

NS_ASSUME_NONNULL_BEGIN

@interface UIViewController (ERPPresent13)

/**
Whether or not to set ModelPresentationStyle automatically for instance, Default is [Class K_automaticallySetModalPresentationStyle].
@return BOOL
*/
@property (nonatomic, assign) BOOL K_automaticallySetModalPresentationStyle;

/**
 Whether or not to set ModelPresentationStyle automatically, Default is YES, but UIImagePickerController/UIAlertController is NO.
 @return BOOL
 */
+ (BOOL)K_automaticallySetModalPresentationStyle;

@end

NS_ASSUME_NONNULL_END

代码语言:javascript
复制
//
//  UIViewController+ERPPresent13.m

#import "UIViewController+ERPPresent13.h"

static const char *K_automaticallySetModalPresentationStyleKey;

@implementation UIViewController (ERPPresent13)
+ (void)load {
    Method originAddObserverMethod = class_getInstanceMethod(self, @selector(presentViewController:animated:completion:));
    Method swizzledAddObserverMethod = class_getInstanceMethod(self, @selector(K_presentViewController:animated:completion:));
    method_exchangeImplementations(originAddObserverMethod, swizzledAddObserverMethod);
}

- (void)setK_automaticallySetModalPresentationStyle:(BOOL)K_automaticallySetModalPresentationStyle {
    objc_setAssociatedObject(self, K_automaticallySetModalPresentationStyleKey, @(K_automaticallySetModalPresentationStyle), OBJC_ASSOCIATION_ASSIGN);
}

- (BOOL)K_automaticallySetModalPresentationStyle {
    id obj = objc_getAssociatedObject(self, K_automaticallySetModalPresentationStyleKey);
    if (obj) {
        return [obj boolValue];
    }
    return [self.class K_automaticallySetModalPresentationStyle];
}

+ (BOOL)K_automaticallySetModalPresentationStyle {
    if ([self isKindOfClass:[UIImagePickerController class]] || [self isKindOfClass:[UIAlertController class]]) {
        return NO;
    }
    return YES;
}

- (void)K_presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion {
    if (@available(iOS 13.0, *)) {
        if (viewControllerToPresent.K_automaticallySetModalPresentationStyle) {
            viewControllerToPresent.modalPresentationStyle = UIModalPresentationFullScreen;
        }
        [self K_presentViewController:viewControllerToPresent animated:flag completion:completion];
    } else {
        // Fallback on earlier versions
        [self K_presentViewController:viewControllerToPresent animated:flag completion:completion];
    }
}

@end


II 上拉加载适配

https://blog.csdn.net/z929118967/article/details/126224009?spm=1001.2014.3001.5501

问题:没有上拉的时候加载更多控件的文案也显示出来了

修复方式1:修改视图距离底部的高度

代码语言:javascript
复制
    [self.vcView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(weakSelf.view).offset(0);
        make.right.equalTo(weakSelf.view).offset(- 0);
        make.top.equalTo(weakSelf.view).offset(0);
        if(isHasSafeAreaInsets()){// 避免没有上拉的时候加载更多控件的文案也显示出来了
            UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
            CGFloat bottom = mainWindow.safeAreaInsets.bottom;
            make.bottom.equalTo(weakSelf.view).offset(bottom);

        }else{
            make.bottom.equalTo(weakSelf.view);
        }
        
        
    }];
    

修复方式2:修改上拉加载控件距离底部的高度 【推荐】

代码语言:javascript
复制

/** 忽略多少scrollView的contentInset的bottom */
//@property (assign, nonatomic) CGFloat ignoredScrollViewContentInsetBottom;

            UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
            CGFloat bottom = mainWindow.safeAreaInsets.bottom;//34
_tableView.mj_footer.ignoredScrollViewContentInsetBottom = isIphoneX ? bottom : 0;

判断安全区域距离

代码语言:javascript
复制
static inline BOOL isIPhoneXSeries() {
    if (@available(iOS 11.0, *)) {
        UIWindow *mainWindow = [[[UIApplication sharedApplication] delegate] window];
        if (mainWindow.safeAreaInsets.bottom > 0.0) {
            return YES;
        }
    }
    return NO;
}


本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2022-08-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 iOS逆向 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • I 下拉刷新适配
    • 1.1 下拉顶部背景色设置
      • 1.2 present 半屏适配
      • II 上拉加载适配
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档