Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WKWebView滚动设置偏移量不生效问题 #459

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,13 @@
#import <MJRefresh/MJRefresh.h>
#import "UIWindow+JXSafeArea.h"

#import <WebKit/WebKit.h>

@interface ListViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, copy) void(^scrollCallback)(UIScrollView *scrollView);

@property (nonatomic, strong) WKWebView *webView;

@end

@implementation ListViewController
Expand All @@ -24,42 +29,50 @@ - (void)dealloc {
- (void)viewDidLoad {
[super viewDidLoad];

_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
self.tableView.backgroundColor = [UIColor whiteColor];
self.tableView.tableFooterView = [UIView new];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
//列表的contentInsetAdjustmentBehavior失效,需要自己设置底部inset
self.tableView.contentInset = UIEdgeInsetsMake(0, 0, UIApplication.sharedApplication.keyWindow.jx_layoutInsets.bottom, 0);
[self.view addSubview:self.tableView];

__weak typeof(self)weakSelf = self;
if (self.isNeedHeader) {
self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.tableView.mj_header endRefreshing];
});
}];
}
if (self.isNeedFooter) {
self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[weakSelf.dataSource addObject:@"加载更多成功"];
[weakSelf.tableView reloadData];
[weakSelf.tableView.mj_footer endRefreshing];
});
}];
}

[self beginFirstRefresh];
}

- (void)viewDidLayoutSubviews {
[super viewDidLayoutSubviews];

self.tableView.frame = self.view.bounds;
}
// _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
// self.tableView.backgroundColor = [UIColor whiteColor];
// self.tableView.tableFooterView = [UIView new];
// self.tableView.dataSource = self;
// self.tableView.delegate = self;
// [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
// //列表的contentInsetAdjustmentBehavior失效,需要自己设置底部inset
// self.tableView.contentInset = UIEdgeInsetsMake(0, 0, UIApplication.sharedApplication.keyWindow.jx_layoutInsets.bottom, 0);
// [self.view addSubview:self.tableView];
//
// __weak typeof(self)weakSelf = self;
// if (self.isNeedHeader) {
// self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [weakSelf.tableView.mj_header endRefreshing];
// });
// }];
// }
// if (self.isNeedFooter) {
// self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// [weakSelf.dataSource addObject:@"加载更多成功"];
// [weakSelf.tableView reloadData];
// [weakSelf.tableView.mj_footer endRefreshing];
// });
// }];
// }
//
// [self beginFirstRefresh];


#warning 调试 WKWebView 滚动导致头视图置顶
self.webView = [[WKWebView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.webView];

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
[self.webView loadRequest:request];
}

//- (void)viewDidLayoutSubviews {
// [super viewDidLayoutSubviews];
//
// self.tableView.frame = self.view.bounds;
//}

- (void)beginFirstRefresh {
if (!self.isHeaderRefreshed) {
Expand All @@ -68,43 +81,43 @@ - (void)beginFirstRefresh {
}

- (void)beginRefreshImmediately {
if (self.isNeedHeader) {
[self.tableView.mj_header beginRefreshing];
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.isHeaderRefreshed = YES;
[self.tableView reloadData];
[self.tableView.mj_header endRefreshing];
});
}else {
self.isHeaderRefreshed = YES;
[self.tableView reloadData];
}
// if (self.isNeedHeader) {
// [self.tableView.mj_header beginRefreshing];
// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
// self.isHeaderRefreshed = YES;
// [self.tableView reloadData];
// [self.tableView.mj_header endRefreshing];
// });
// }else {
// self.isHeaderRefreshed = YES;
// [self.tableView reloadData];
// }
}

#pragma mark - UITableViewDataSource, UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
if (!self.isHeaderRefreshed) {
return 0;
}
return self.dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
cell.textLabel.text = self.dataSource[indexPath.row];
return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
return 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
DetailViewController *detailVC = [[DetailViewController alloc] init];
detailVC.infoString = self.dataSource[indexPath.row];
[self.navigationController pushViewController:detailVC animated:YES];
}
//- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// if (!self.isHeaderRefreshed) {
// return 0;
// }
// return self.dataSource.count;
//}
//
//- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
// UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
// cell.textLabel.text = self.dataSource[indexPath.row];
// return cell;
//}
//
//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
// return 50;
//}
//
//- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// DetailViewController *detailVC = [[DetailViewController alloc] init];
// detailVC.infoString = self.dataSource[indexPath.row];
// [self.navigationController pushViewController:detailVC animated:YES];
//}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
!self.scrollCallback ?: self.scrollCallback(scrollView);
Expand All @@ -117,7 +130,8 @@ - (UIView *)listView {
}

- (UIScrollView *)listScrollView {
return self.tableView;
// return self.tableView;
return self.webView.scrollView;
}

- (void)listViewDidScrollCallback:(void (^)(UIScrollView *))callback {
Expand Down
4 changes: 3 additions & 1 deletion Sources/JXPagerView/JXPagerView.m
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ - (void)setMainTableViewToMaxContentOffsetY {
}

- (void)setListScrollViewToMinContentOffsetY:(UIScrollView *)scrollView {
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, [self minContentOffsetYInListScrollView:scrollView]);
dispatch_async(dispatch_get_main_queue(), ^{
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, [self minContentOffsetYInListScrollView:scrollView]);
});
}

- (CGFloat)minContentOffsetYInListScrollView:(UIScrollView *)scrollView {
Expand Down