每当我在我的应用程序中使用的UIWebView中重新加载网站时,网站就会向下移动。我不知道为什么会发生这种情况,因为当我在Safari中访问该网站的移动版本时,该网站不会向下滚动。
以下是UIWebView的代码:
import Foundation
import UIKit
import SVProgressHUD
import Canvas
class WebViewHome: UIViewController, UIWebViewDelegate, SWRevealViewControllerDelegate{
@IBOutlet weak var heightConstraint: NSLayoutConstraint!
@IBOutlet weak var WebViewContainer: UIView!
@IBOutlet weak var WebViewTst: UIWebView!
@IBOutlet weak var animView: CSAnimationView!
override func viewDidLoad() {
super.viewDidLoad()
self.navigationController?.navigationBar.topItem!.title = "Home"
let URL = NSURL(string: "https://gunnoracle.com/")
WebViewTst.loadRequest(NSURLRequest(url: URL! as URL) as URLRequest)
let topSpace = -190
let bottomSpace = -3045
let leftSpace = -40
let rightSpace = -40
WebViewTst.scrollView.contentInset = UIEdgeInsets(top: CGFloat(topSpace), left: CGFloat(leftSpace), bottom: CGFloat(bottomSpace), right: CGFloat(rightSpace))
WebViewTst.scrollView.bounces = false
WebViewTst.scrollView.maximumZoomScale = 1.0
WebViewTst.scrollView.minimumZoomScale = 1.0
WebViewTst.delegate = self
}
override func viewDidAppear(_ animated: Bool) {
self.revealViewController().delegate = self
}
func revealController(_ revealController: SWRevealViewController!, didMoveTo position: FrontViewPosition) {
if(revealController.frontViewPosition == FrontViewPosition.right){
animView.isHidden = true
UIView.animate(withDuration: 0.5, animations: {
self.WebViewTst.layer.zPosition = 1
self.WebViewTst.transform = CGAffineTransform(translationX: 0, y: -65);
self.view.layoutIfNeeded()
})
}
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool {
switch navigationType {
case .linkClicked:
let topSpace = -170
let bottomSpace = -2840
let leftSpace = -40
let rightSpace = -40
animView.startCanvasAnimation()
WebViewTst.scrollView.contentInset = UIEdgeInsets(top: CGFloat(topSpace), left: CGFloat(leftSpace), bottom: CGFloat(bottomSpace), right: CGFloat(rightSpace))
WebViewTst.scrollView.bounces = false
WebViewTst.scrollView.maximumZoomScale = 1.0
WebViewTst.scrollView.minimumZoomScale = 1.0
WebViewTst.delegate = self
default:
break
}
return true
}
func webViewDidStartLoad(_ webView: UIWebView) {
SVProgressHUD.show(withStatus: "Loading...")
SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.dark)
SVProgressHUD.setRingThickness(1.0)
NSLog("Webview has started loading")
}
func webViewDidFinishLoad(_ webView: UIWebView) {
SVProgressHUD.dismiss()
NSLog("Webview has successfully loaded")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:1)
这是对该问题的评论的跟进。
请注意,此代码未经过测试,因此只能用作指南。
要对抗顶部的内容并保持滚动位置,您可以使用类似于
的代码- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
CGFloat currentYOffset = scrollView.contentOffset.y;
CGFloat topIntest = scrollView.contentInset.top;
scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x, currentYOffset - topIntest);
}
如果没有看到您的确切项目和预期的视觉输出,很难给出完美的答案。
理想情况下,您应该稍微改变一下这个解决方案。您上面的代码使用的是固定的contentInset
,它告诉我您需要让您的页面具有特定的填充才能正确显示。您应该采取哪些措施来确保将性能保持在最高级别,即将填充实现到您正在加载的网页中。
如果您有权访问网页源代码,请使用body标签上的CSS填充样式来复制所需的间距/填充/对齐。
如果您无权访问网页源代码,您也可以使用
将网页加载到字符串中NSString * content = [NSString stringWithContentsOfURL:@" http://example.com" encoding:NSUTF8StringEncoding error:nil];
然后,您可以将自己的样式注入HTML。
有很多方法可以做到这一点,例如:
NSURL *contentURL = [NSURL URLWithString:@"http://example.com"];
//load content to string
NSString *content = [NSString stringWithContentsOfURL:contentURL encoding:NSUTF8StringEncoding error:nil];
//create css style to inject
NSString *paddingCSS = @"<style> body { margin-top: -190px; margin-left:-40px; margin-right:-40px; margin-bottom:-3045px } </style></head>";
//inject the string just before the </head> tag
[content stringByReplacingOccurrencesOfString:@"</head>" withString:paddingCSS options:NSCaseInsensitiveSearch range:NSRangeFromString(content)];
//load the webview using the content string.
//Set the baseURL to ensure relative links etc. still work
[self.webView loadHTMLString:content baseURL:contentURL];