ios 怎样使tableviewhead随着tableview的拉伸变大
1个回答
展开全部
你的目的是下拉的时候避免出现UITableView的背景吧,导致颜色、样式的不同意。
如果是这个目的可以这么写一个Cagetory
.h
#import <UIKit/UIKit.h>
@interface UITableView (configure)
- (UIView *)topBackgroundViewWith:(UIColor *)color;
@property (nonatomic, strong) UIView *topBackgroundView;
@end
.m
#import "UITableView+configure.h"
#import <objc/runtime.h>
@implementation UITableView (configure)
- (UIView *)topBackgroundViewWith:(UIColor *)color {
self.backgroundColor = [UIColor clearColor];
if (self.backgroundView == nil) {
self.backgroundView = [[UIView alloc] init];
}
UIView *topBackgroundView = [[UIView alloc] init];
topBackgroundView.backgroundColor = color;
[self.backgroundView addSubview:topBackgroundView];
self.topBackgroundView = topBackgroundView;
[self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
return topBackgroundView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"]) {
self.topBackgroundView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), MAX(self.contentInset.top - self.contentOffset.y, 0));
}
}
static const void *topBackgroundViewKey = &topBackgroundViewKey;
- (void)setTopBackgroundView:(UIView *)topBackgroundView {
objc_setAssociatedObject(self, &topBackgroundViewKey, topBackgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)topBackgroundView {
return objc_getAssociatedObject(self, &topBackgroundViewKey);
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"contentOffset"];
}
@end
如果是这个目的可以这么写一个Cagetory
.h
#import <UIKit/UIKit.h>
@interface UITableView (configure)
- (UIView *)topBackgroundViewWith:(UIColor *)color;
@property (nonatomic, strong) UIView *topBackgroundView;
@end
.m
#import "UITableView+configure.h"
#import <objc/runtime.h>
@implementation UITableView (configure)
- (UIView *)topBackgroundViewWith:(UIColor *)color {
self.backgroundColor = [UIColor clearColor];
if (self.backgroundView == nil) {
self.backgroundView = [[UIView alloc] init];
}
UIView *topBackgroundView = [[UIView alloc] init];
topBackgroundView.backgroundColor = color;
[self.backgroundView addSubview:topBackgroundView];
self.topBackgroundView = topBackgroundView;
[self addObserver:self forKeyPath:@"contentOffset" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
return topBackgroundView;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"contentOffset"]) {
self.topBackgroundView.frame = CGRectMake(0, 0, CGRectGetWidth(self.frame), MAX(self.contentInset.top - self.contentOffset.y, 0));
}
}
static const void *topBackgroundViewKey = &topBackgroundViewKey;
- (void)setTopBackgroundView:(UIView *)topBackgroundView {
objc_setAssociatedObject(self, &topBackgroundViewKey, topBackgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (UIView *)topBackgroundView {
return objc_getAssociatedObject(self, &topBackgroundViewKey);
}
- (void)dealloc {
[self removeObserver:self forKeyPath:@"contentOffset"];
}
@end
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询