gpt4 book ai didi

ios - 刷新时的 UIWebView 网站向下移动 - Swift

转载 作者:搜寻专家 更新时间:2023-11-01 07:15:45 27 4
gpt4 key购买 nike

每当我在我的应用程序中使用的 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.
}

最佳答案

这是对问题下评论的跟进。

请注意,此代码未经测试,因此只能用作指南。

为了抵消顶部的内容插入并保持滚动位置,您可以使用类似于

的代码
- (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];

然后您可以将自己的样式注入(inject) 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];

关于ios - 刷新时的 UIWebView 网站向下移动 - Swift,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42285823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com