You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

设置更窄的UIRefreshControl刷新状态高度

调整UIRefreshControl刷新时的高度为44点(Objective-C方案)

嘿,我来给你分享几个可行的Objective-C方案,解决UIRefreshControl刷新时高度默认60点、想改成44点的问题——毕竟系统没给公开API直接设置这个高度,得用点变通的办法:

方案一:通过KVO监听刷新状态调整高度

这种方法是监听UIRefreshControl的isRefreshing属性,在状态变化时手动修改它的frame,同时同步调整tableView的contentInset避免内容遮挡:

  1. 初始化并添加监听
- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init];
    [refreshControl addTarget:self action:@selector(handleRefresh:) forControlEvents:UIControlEventValueChanged];
    [self.tableView addSubview:refreshControl];
    self.refreshControl = refreshControl;
    
    // 添加KVO监听刷新状态
    [refreshControl addObserver:self forKeyPath:@"isRefreshing" options:NSKeyValueObservingOptionNew context:nil];
}
  1. 实现KVO回调处理高度调整
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
    if ([keyPath isEqualToString:@"isRefreshing"]) {
        BOOL isRefreshing = [change[NSKeyValueChangeNewKey] boolValue];
        UIRefreshControl *rc = (UIRefreshControl *)object;
        
        if (isRefreshing) {
            // 刷新中,设置高度为44点
            CGRect newFrame = rc.frame;
            newFrame.size.height = 44.0f;
            rc.frame = newFrame;
            
            // 同步调整tableView的contentInset,避免内容被顶上去太多
            UIEdgeInsets inset = self.tableView.contentInset;
            inset.top = 44.0f;
            self.tableView.contentInset = inset;
        } else {
            // 刷新结束,恢复contentInset
            UIEdgeInsets inset = self.tableView.contentInset;
            inset.top = 0.0f;
            self.tableView.contentInset = inset;
        }
    }
}
  1. 记得在销毁时移除观察者
- (void)dealloc {
    [self.refreshControl removeObserver:self forKeyPath:@"isRefreshing"];
}

方案二:自定义UIRefreshControl子类

如果不想用KVO,可以自定义子类重写相关方法,确保刷新时高度始终是44点:

// CustomRefreshControl.h
#import <UIKit/UIKit.h>

@interface CustomRefreshControl : UIRefreshControl
@end

// CustomRefreshControl.m
#import "CustomRefreshControl.h"

@implementation CustomRefreshControl

- (void)setRefreshing:(BOOL)refreshing {
    [super setRefreshing:refreshing];
    if (refreshing) {
        // 刷新状态下强制设置高度
        CGRect frame = self.frame;
        frame.size.height = 44.0f;
        self.frame = frame;
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    // 系统布局后再次检查,防止被覆盖
    if (self.isRefreshing) {
        CGRect frame = self.frame;
        frame.size.height = 44.0f;
        self.frame = frame;
    }
}

@end

使用时直接替换系统的UIRefreshControl就行,同样要记得在ViewController里调整tableView的contentInset哦~

注意:这些都是基于系统现有API的变通方案,因为UIRefreshControl的内部布局是系统管控的,未来iOS版本更新可能会有变动,但目前在大部分版本里都是有效的。

内容的提问来源于stack exchange,提问作者Alan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 06:17:22