UIRefreshControl incorrect title offset during first run and sometimes title missing

前端 未结 8 957
自闭症患者
自闭症患者 2020-12-04 15:46

The text is offset wrong by the first launch of UIRefreshControl... later sometimes the refresh text doesn\'t show up at all and just the spiny is visible

I don\'t t

8条回答
  •  -上瘾入骨i
    2020-12-04 16:30

    I finally found the holy grail on this, which looks working in all cases

    note : UIRefreshControl is added to a UITableViewController (note, never add UIRefreshControl just as subview to a normal UIVIewController's UITableView) (best to add UITableViewController as a child VC inside a UIViewController if you must)

    note : that this also fixes the problem, that the UIRefreshControl is not vissible at first refresh (link)

    Add to you .h

    @interface MyViewController ()
    
    @property (nonatomic, assign) BOOL refreshControlFixApplied;
    
    - (void)beginRefreshing;
    - (void)beginRefreshingWithText:(NSString *)text;
    - (void)endRefreshing;
    - (void)endRefreshingWithText:(NSString *)text;
    
    @end
    

    Add to you .m

    ////////////////////////////////////////////////////////////////////////
    #pragma mark - UIRefreshControl Fix (peter@min60.com) https://stackoverflow.com/questions/19121276/uirefreshcontrol-incorrect-title-offset-during-first-run-and-sometimes-title-mis/
    ////////////////////////////////////////////////////////////////////////
    
    - (void)beginRefreshingWithText:(NSString *)text {
    
        [self setRefreshControlText:text];
        [self beginRefreshing];
    
    }
    
    - (void)endRefreshingWithText:(NSString *)text {
    
        [self setRefreshControlText:text];
        [self.refreshControl endRefreshing];
    
    }
    
    - (void)beginRefreshing {
    
        if (self.refreshControl == nil) {
            return;
        }
    
        if (!self.refreshControlFixApplied) {
    
            dispatch_async(dispatch_get_main_queue(), ^{
    
                if ([self.refreshControl.attributedTitle length] == 0) {
                    [self setRefreshControlText:@" "];
                }
                [self.refreshControl beginRefreshing];
    
                dispatch_async(dispatch_get_main_queue(), ^{
    
                    [self.refreshControl endRefreshing];
    
                    dispatch_async(dispatch_get_main_queue(), ^{
    
                        // set the title before calling beginRefreshing
                        if ([self.refreshControl.attributedTitle length] == 0) {
                            [self setRefreshControlText:@" "];
                        }
                        if (self.tableView.contentOffset.y == 0) {
                            self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
                        }
                        [self.refreshControl beginRefreshing];
    
                        self.refreshControlFixApplied = YES;
    
                    });
    
                });
    
            });
    
        } else {
    
            if (self.tableView.contentOffset.y == 0) {
                self.tableView.contentOffset = CGPointMake(0, -self.refreshControl.frame.size.height);
            }
            [self.refreshControl beginRefreshing];
    
        }
    
    }
    
    - (void)endRefreshing {
    
        if (self.refreshControl == nil) {
            return;
        }
    
        if (!self.refreshControlFixApplied) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [self endRefreshing];
            });
        } else {
            if (self.tableView.contentOffset.y < 0) {
                self.tableView.contentOffset = CGPointMake(0, 0);
            }
            [self.refreshControl endRefreshing];
    
        }
    
    }
    
    - (void)setRefreshControlText:(NSString *)text {
    
        UIFont * font = [UIFont fontWithName:@"Helvetica-Light" size:10.0];
        NSDictionary *attributes = @{NSFontAttributeName : font, NSForegroundColorAttributeName : [UIColor colorWithHex:0x00B92E]};
        self.refreshControl.attributedTitle = [[NSAttributedString alloc] initWithString:text attributes:attributes];
    
    }
    

    Use only methods

    - (void)beginRefreshing;
    - (void)beginRefreshingWithText:(NSString *)text;
    - (void)endRefreshing;
    - (void)endRefreshingWithText:(NSString *)text;
    

提交回复
热议问题