Skip to content

Commit da669f1

Browse files
committed
WKWebView + UITableView
1 parent b1d4ddf commit da669f1

File tree

3 files changed

+15
-12
lines changed

3 files changed

+15
-12
lines changed

iOS_Tips/DarkMode/WKWebView/SLWebTableViewController.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ NS_ASSUME_NONNULL_BEGIN
1212

1313
/*
1414
WKWebView + UITableView
15-
方案一:WebView作为TableView的Header,但不撑开webView
15+
方案一:WebView作为TableView的Header, 撑开webView
1616
*/
1717
@interface SLWebTableViewController : UIViewController
1818

iOS_Tips/DarkMode/WKWebView/SLWebTableViewController.m

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ @interface SLWebTableViewController ()<UITableViewDelegate,UITableViewDataSource
1414
@property (nonatomic, strong) WKWebView * webView;
1515
///网页加载进度视图
1616
@property (nonatomic, strong) UIProgressView * progressView;
17-
/// WKWebView 内容的高度
17+
/// WKWebView 内容的高度 默认屏幕高
1818
@property (nonatomic, assign) CGFloat webContentHeight;
1919

2020
@property (nonatomic, strong) UITableView *tableView;
@@ -44,9 +44,8 @@ - (void)setupUi {
4444
[self configureWebTable];
4545
}
4646
- (void)configureWebTable {
47+
self.webView.sl_h = _webContentHeight == 0 ? SL_kScreenHeight : _webContentHeight;
4748
self.tableView.tableHeaderView = self.webView;
48-
self.tableView.bounces = NO;
49-
self.webView.scrollView.bounces = NO;
5049
}
5150

5251
#pragma mark - Getter
@@ -122,6 +121,7 @@ -(void)observeValueForKeyPath:(NSString *)keyPath
122121
}else if ([keyPath isEqualToString:NSStringFromSelector(@selector(contentSize))]
123122
&& object == _webView.scrollView && _webContentHeight != _webView.scrollView.contentSize.height) {
124123
_webContentHeight = _webView.scrollView.contentSize.height;
124+
[self configureWebTable];
125125
NSLog(@"WebViewContentSize = %@",NSStringFromCGSize(_webView.scrollView.contentSize))
126126
}
127127
}
@@ -130,6 +130,9 @@ -(void)observeValueForKeyPath:(NSString *)keyPath
130130

131131
#pragma mark - HelpMethods
132132

133+
#pragma mark - UIScrollViewDelegate
134+
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
135+
}
133136
#pragma mark - UITableViewDelegate,UITableViewDataSource
134137
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
135138
return 1;

iOS_Tips/DarkMode/WKWebView/SLWebViewController.m

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ @implementation SLWebViewController
2525
#pragma mark - Override
2626
- (void)viewDidLoad {
2727
[super viewDidLoad];
28-
29-
SLWebTableViewController *webTableViewController = [[SLWebTableViewController alloc] init];
30-
[self.navigationController pushViewController:webTableViewController animated:YES];
31-
return;
32-
3328
[self setupUI];
3429
[self addKVO];
3530
}
@@ -42,13 +37,13 @@ - (void)dealloc {
4237
NSLog(@"%@释放了",NSStringFromClass(self.class));
4338
}
4439

45-
4640
#pragma mark - UI
4741
- (void)setupUI {
4842
self.view.backgroundColor = UIColor.whiteColor;
43+
UIBarButtonItem *testItem = [[UIBarButtonItem alloc] initWithTitle:@"Test" style:UIBarButtonItemStyleDone target:self action:@selector(testAction:)];
4944
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"上一步" style:UIBarButtonItemStyleDone target:self action:@selector(goBackAction:)];
5045
UIBarButtonItem *forwardItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(goForwardAction:)];
51-
self.navigationItem.rightBarButtonItems = @[forwardItem,backItem];
46+
self.navigationItem.rightBarButtonItems = @[forwardItem,backItem,testItem];
5247

5348
[self.view addSubview:self.webView];
5449

@@ -62,7 +57,7 @@ - (WKWebView *)webView {
6257

6358
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, SL_kScreenHeight) configuration:config];
6459

65-
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://baidu.com"]];
60+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://www.baidu.com"]];
6661
[_webView loadRequest:request];
6762
}
6863
return _webView;
@@ -133,4 +128,9 @@ - (void)goForwardAction:(id)sender{
133128
[_webView goForward];
134129
}
135130

131+
- (void)testAction:(id)sender {
132+
SLWebTableViewController *webTableViewController = [[SLWebTableViewController alloc] init];
133+
[self.navigationController pushViewController:webTableViewController animated:YES];
134+
}
135+
136136
@end

0 commit comments

Comments
 (0)