Medium上有一篇文章說用_variable取代self.variable 就不會循環引用
實驗之後發現:
_variable也會
還是宣告成weak最好
既使weakSelf去呼叫一個方法,方法裡面有不是weak的self,那還是不會循環引用的。
用變數型態直接宣告在.h或.m,就算加了__weak還是一樣會循環引用,得要改成property的型態並宣告成weak才能避免
如果你在block中使用一些singleton例如[UIApplication sharedApplication],只要不要用變數去接它,就可以安全下莊
2018年5月29日 星期二
2018年5月25日 星期五
WKWebView
// Inject新的attribute給你的wkwebview
- (void)setupWebView {
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('initial-scale', '1'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:userScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = userContentController;
// 因為pdf的高度用webview的寬度去調整,所以要先設定好。
CGRect initialWebViewFrame = self.view.frame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat height = initialWebViewFrame.size.height;
CGFloat width = initialWebViewFrame.size.width;
if (height > width) {
initialWebViewFrame.size = CGSizeMake(width, 0);
} else {
initialWebViewFrame.size = CGSizeMake(height, 0);
}
}
_webView = [[WKWebView alloc] initWithFrame:initialWebViewFrame configuration:wkWebConfig];
_webView.scrollView.contentSize = initialWebViewFrame.size;
[_webViewContainer addSubview:_webView];
_webView.navigationDelegate = self;
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
_webView.scrollView.scrollEnabled = NO;
[_webView.scrollView setBackgroundColor:[UIColor clearColor]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
} else {
if (shouldReloadFlag) {
shouldReloadFlag = NO;
[self loadWebView];
}
}
[self resizeScrollView];
}
- (void)resizeScrollView {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
if (_webView.isLoading == false) {
for (UIView *subview in self.webView.scrollView.subviews) {
subview.backgroundColor = [UIColor clearColor];
// 取得pdf的高
if ([[[subview class] description] isEqualToString:@"WKPDFView"]) {
NSLog(@"pdfview.size: %@ webviewContainer.size:%@", NSStringFromCGSize(subview.frame.size), NSStringFromCGSize(_webViewContainer.frame.size));
[self resizeScrollViewByWebViewContentHeight:subview.frame.size.height / subview.frame.size.width * _webViewContainer.frame.size.width];
break;
}
}
}
} else {
if (_webView.isLoading == false) {
__weak typeof(self) weakSelf = self;
[_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id result, NSError * _Nullable error) {
CGFloat height = [result floatValue] + 30.0;
[weakSelf resizeScrollViewByWebViewContentHeight:height];
}];
}
}
}
- (void)setupWebView {
NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('initial-scale', '1'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
WKUserScript *userScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:userScript];
WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
wkWebConfig.userContentController = userContentController;
// 因為pdf的高度用webview的寬度去調整,所以要先設定好。
CGRect initialWebViewFrame = self.view.frame;
if ([[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortrait ||
[[UIApplication sharedApplication] statusBarOrientation] == UIInterfaceOrientationPortraitUpsideDown) {
CGFloat height = initialWebViewFrame.size.height;
CGFloat width = initialWebViewFrame.size.width;
if (height > width) {
initialWebViewFrame.size = CGSizeMake(width, 0);
} else {
initialWebViewFrame.size = CGSizeMake(height, 0);
}
}
_webView = [[WKWebView alloc] initWithFrame:initialWebViewFrame configuration:wkWebConfig];
_webView.scrollView.contentSize = initialWebViewFrame.size;
[_webViewContainer addSubview:_webView];
_webView.navigationDelegate = self;
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTop multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeHeight multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeLeading multiplier:1.0 constant:0]];
[_webViewContainer addConstraint:[NSLayoutConstraint constraintWithItem:_webView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:_webViewContainer attribute:NSLayoutAttributeTrailing multiplier:1.0 constant:0]];
_webView.scrollView.scrollEnabled = NO;
[_webView.scrollView setBackgroundColor:[UIColor clearColor]];
[_webView setBackgroundColor:[UIColor clearColor]];
[_webView setOpaque:NO];
}
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
} else {
if (shouldReloadFlag) {
shouldReloadFlag = NO;
[self loadWebView];
}
}
[self resizeScrollView];
}
- (void)resizeScrollView {
if ([[SettingsManager sharedManager].agreement[@"agreement_type"] isEqualToString:@"pdf"]) {
if (_webView.isLoading == false) {
for (UIView *subview in self.webView.scrollView.subviews) {
subview.backgroundColor = [UIColor clearColor];
// 取得pdf的高
if ([[[subview class] description] isEqualToString:@"WKPDFView"]) {
NSLog(@"pdfview.size: %@ webviewContainer.size:%@", NSStringFromCGSize(subview.frame.size), NSStringFromCGSize(_webViewContainer.frame.size));
[self resizeScrollViewByWebViewContentHeight:subview.frame.size.height / subview.frame.size.width * _webViewContainer.frame.size.width];
break;
}
}
}
} else {
if (_webView.isLoading == false) {
__weak typeof(self) weakSelf = self;
[_webView evaluateJavaScript:@"document.body.offsetHeight" completionHandler:^(id result, NSError * _Nullable error) {
CGFloat height = [result floatValue] + 30.0;
[weakSelf resizeScrollViewByWebViewContentHeight:height];
}];
}
}
}
2018年5月19日 星期六
keyboard disappaer when tableView scrolling
訂閱:
文章 (Atom)