gpt4 book ai didi

ios - Xcode/Swift 在键盘打开时调整 UIWebView 的大小

转载 作者:行者123 更新时间:2023-11-28 07:42:42 27 4
gpt4 key购买 nike

好吧,我在这上面花了好几个小时,可能只是想念它...但是当单击文本字段并打开键盘时,如何调整 UIWebView 的大小?

现在我正在听键盘,获取所有测量值以调整它的大小,然后调整它的大小:

keyboardWillShow()

在模拟器中,它完全按预期工作,然后在带有“.trailing”属性的 UIButtonBarStackView 上显示约束错误后恢复原状。我假设这是来自键盘,但我不确定......

有谁知道如何解决这个问题,或者去哪里找?

我已经看到了其他几个关于它的问题并尝试了其他解决方案但没有任何运气。

最佳答案

Swift 4.2 回答:(虽然 webView 已被弃用,但我建议使用 wekKitView)

import UIKit
import AVFoundation

class ViewController: UIViewController {

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {
super.viewDidLoad()

let url = URL(string: "https://www.google.com")
let request = URLRequest(url: url!)
webView.loadRequest(request)

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(notification:)), name: UIApplication.keyboardWillShowNotification, object: nil)

NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(notification:)), name: UIApplication.keyboardWillHideNotification, object: nil)
}

@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardHeight = (notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue.height {

webView.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardHeight, right: 0)
}
}

@objc func keyboardWillHideß(notification: NSNotification) {
UIView.animate(withDuration: 0.2, animations: {
// For some reason adding inset in keyboardWillShow is animated by itself but removing is not, that's why we have to use animateWithDuration here
self.webView.scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
})
}
}

关于ios - Xcode/Swift 在键盘打开时调整 UIWebView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51780871/

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