iOS swift5.x KafkaRefresh刷新不居中问题
2019-12-10 本文已影响0人
Bruce_XHG
如果设置了tableview、collectionview、scrollview的内边距,刚好你使用的又是KafkaRefresh;那么你可能会遇到刷新不居中的问题。
首先我们看一下KafkaRefresh源码
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
if (self.superview && newSuperview == nil) {
if (_observering) {
[self.superview removeObserver:self forKeyPath:KafkaContentOffset];
[self.superview removeObserver:self forKeyPath:KafkaContentSize];
_observering = NO;
}
}
else if (self.superview == nil && newSuperview) {
if (!_observering) {
_scrollView = (UIScrollView *)newSuperview;
/**
sometimes, this method called before `layoutSubviews`,such as UICollectionViewController
*/
[self layoutIfNeeded];
_presetContentInsets = ((UIScrollView *)newSuperview).realContentInset;
[newSuperview addObserver:self forKeyPath:KafkaContentOffset options:options context:nil];
[newSuperview addObserver:self forKeyPath:KafkaContentSize options:options context:nil];
_observering = YES;
}
}
}
再看一下修改的代码
- (void)willMoveToSuperview:(UIView *)newSuperview {
[super willMoveToSuperview:newSuperview];
NSKeyValueObservingOptions options = NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld;
if (self.superview && newSuperview == nil) {
if (_observering) {
[self.superview removeObserver:self forKeyPath:KafkaContentOffset];
[self.superview removeObserver:self forKeyPath:KafkaContentSize];
_observering = NO;
}
}
else if (self.superview == nil && newSuperview) {
if (!_observering) {
_scrollView = (UIScrollView *)newSuperview;
self.kr_left = -_scrollView.realContentInset.left;
/**
sometimes, this method called before `layoutSubviews`,such as UICollectionViewController
*/
[self layoutIfNeeded];
_presetContentInsets = ((UIScrollView *)newSuperview).realContentInset;
[newSuperview addObserver:self forKeyPath:KafkaContentOffset options:options context:nil];
[newSuperview addObserver:self forKeyPath:KafkaContentSize options:options context:nil];
_observering = YES;
}
}
}
其实就是添加了一句代码
self.kr_left = -_scrollView.realContentInset.left;