gpt4 book ai didi

ios - 在 Swift 的 UIWebView 中替换键盘的 inputAccessoryView

转载 作者:搜寻专家 更新时间:2023-10-30 22:15:23 25 4
gpt4 key购买 nike

几天来我一直在寻找一个简单的答案,但我似乎找不到。

如果键盘上存在辅助 View ,我想查找并替换附加到我的 UIWebView 的键盘的 inputAccessoryView。我想替换它,这样我就可以拥有自己的 Prev 和 Next 按钮。快速完成是一个优势。

听起来很简单,但并不明显。

最佳答案

import UIKit

extension UIViewController {

func addNewAccessoryView(oldAccessoryView:UIView)
{
var frame = oldAccessoryView.frame;
var newAccessoryView = UIView(frame:frame)
newAccessoryView.backgroundColor = UIColor.lightGrayColor()

let fn = ".HelveticaNeueInterface-Bold"
var doneButton = UIButton(frame:CGRectMake(frame.size.width-80,6,100,30))
doneButton.setTitle("Done", forState: UIControlState.Normal)
doneButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
doneButton.titleLabel!.font = UIFont(name: fn, size: 15)
doneButton.addTarget(self, action: "buttonAccessoryDoneAction:", forControlEvents: UIControlEvents.TouchUpInside)

var prevButton = UIButton(frame:CGRectMake(2,6,50,30))
prevButton.setTitle("<", forState: UIControlState.Normal)
prevButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
prevButton.titleLabel!.font = UIFont(name: fn, size: 15)
prevButton.addTarget(self, action: "buttonAccessoryPrevAction:", forControlEvents: UIControlEvents.TouchUpInside)

var nextButton = UIButton(frame:CGRectMake(35,6,50,30))
nextButton.setTitle(">", forState: UIControlState.Normal)
nextButton.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
nextButton.titleLabel!.font = UIFont(name: fn, size: 15)
nextButton.addTarget(self, action: "buttonAccessoryNextAction:", forControlEvents: UIControlEvents.TouchUpInside)

newAccessoryView.addSubview(prevButton);
newAccessoryView.addSubview(nextButton);
newAccessoryView.addSubview(doneButton);

oldAccessoryView.addSubview(newAccessoryView)

}

func traverseSubViews(vw:UIView) -> UIView
{
if (vw.description.hasPrefix("<UIWebFormAccessory")) {
return vw
}

for(var i = 0 ; i < vw.subviews.count; i++) {
var subview = vw.subviews[i] as UIView;
if (subview.subviews.count > 0) {
var subvw = self.traverseSubViews(subview)
if (subvw.description.hasPrefix("<UIWebFormAccessory")) {
return subvw
}
}
}
return UIView()
}

func replaceKeyboardInputAccessoryView()
{
// locate accessory view
let windowCount = UIApplication.sharedApplication().windows.count
if (windowCount < 2) {
return;
}

let tempWindow:UIWindow = UIApplication.sharedApplication().windows[1] as UIWindow
var accessoryView:UIView = traverseSubViews(tempWindow)
if (accessoryView.description.hasPrefix("<UIWebFormAccessory")) {
// Found the inputAccessoryView UIView
if (accessoryView.subviews.count > 0) {
self.addNewAccessoryView(accessoryView)
}
}
}

// Override these in your UIViewController
func buttonAccessoryNextAction(sender:UIButton!)
{
println("Next Clicked")
}

func buttonAccessoryPrevAction(sender:UIButton!)
{
println("Prev Clicked")
}

func buttonAccessoryDoneAction(sender:UIButton!)
{
println("Done Clicked")
}
}

然后在您的 UIViewController 中设置一个 Notification 当键盘出现时。您可以在 viewDidAppear() 中执行此操作。

    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardDidShow:"), name:UIKeyboardDidShowNotification, object: nil);

并从您的 keyboardDidShow 处理程序中调用 UIViewControllerreplaceKeyboardInputAccessoryView 方法。

func keyboardDidShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
self.replaceKeyboardInputAccessoryView()
}
}

关于ios - 在 Swift 的 UIWebView 中替换键盘的 inputAccessoryView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30312525/

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