在Swift的UIWebView中替换键盘的inputAccessoryView

时间:2015-05-18 20:45:42

标签: ios swift uiwebview keyboard inputaccessoryview

我一直在寻找简单回答的日子,但我似乎找不到它。

如果键盘上存在附件视图,我想找到并替换附加到我的UIWebView的键盘的inputAccessoryView。我想替换它,所以我可以拥有自己的Prev和Next按钮。在swift中这样做是有利的。

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

4 个答案:

答案 0 :(得分:3)

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处理程序中调用replaceKeyboardInputAccessoryView的{​​{1}}方法。

UIViewController

答案 1 :(得分:0)

user3670534解决方案工作正常,但如果更改了Windows的顺序,您可以使用以下内容来获取正确的窗口:

var tempWindow: UIWindow!;
for window in UIApplication.sharedApplication().windows {
    if (window.description.hasPrefix("<UITextEffectsWindow")) {
        tempWindow = window
    }
}

答案 2 :(得分:0)

我发布了一个要点来完成这个: https://gist.github.com/kgaidis/5f9a8c7063b687cc3946fad6379c1a66

它属于UIWebView类别,您所做的就是更改customInputAccessoryView属性:

@interface UIWebView (CustomInputAccessoryView)

@property (strong, nonatomic) UIView *customInputAccessoryView;

@end

请注意,这会使用私有API,因此请自行承担风险,但似乎很多应用程序仍会执行类似的操作。

答案 3 :(得分:0)

在Swift 3中

我删除了内置的Accessory视图,然后使用XIB构建了自己的视图。 (谢谢@Rob) 这显示了如何删除内置视图:

<强> WebViewExtensions.swift:

import UIKit

extension UIViewController {
    func removeInputAccessoryView() {
        // locate accessory view
        let windowCount = UIApplication.shared.windows.count
        if (windowCount < 2) {
            return;
        }

        let tempWindow:UIWindow = UIApplication.shared.windows[1] as UIWindow
        let accessoryView:UIView = traverseSubViews(vw: tempWindow)
        if (accessoryView.description.hasPrefix("<UIWebFormAccessory")) {
            // Found the inputAccessoryView UIView
            accessoryView.removeFromSuperview()
        }
    }

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

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

<强> WKWebView

override func viewDidLoad() {
        super.viewDidLoad()

        NotificationCenter.default.addObserver(forName: NSNotification.Name.UIKeyboardDidShow, object: nil, queue: nil) { (notification) in                

            if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {                    
                self.removeInputAccessoryView()
            }
        }

    }

通过使用上述解决方案,可以对其进行修改。