6

簡単な答えを何日も探していましたが、見つけられないようです。

キーボードにアクセサリ ビューが存在する場合、UIWebView に接続されているキーボードの inputAccessoryView を見つけて置き換えたいと考えています。独自の [Prev] ボタンと [Next] ボタンを使用できるように置き換えたいと考えています。迅速にそれを行うことはプラスです。

簡単に聞こえますが、明らかではありません。

4

4 に答える 4

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メソッドを呼び出しますUIViewController

func keyboardDidShow(notification: NSNotification) {
    if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue() {
        self.replaceKeyboardInputAccessoryView()
    }
}
于 2015-05-19T23:13:14.420 に答える
0

user3670534 ソリューションは正常に機能しますが、ウィンドウの順序が変更された場合は、次を使用して正しいウィンドウを取得できます。

var tempWindow: UIWindow!;
for window in UIApplication.sharedApplication().windows {
    if (window.description.hasPrefix("<UITextEffectsWindow")) {
        tempWindow = window
    }
}
于 2016-03-24T20:22:37.763 に答える
0

スウィフト3では

組み込みのアクセサリ ビューを削除し、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()
            }
        }

    }

上記のソリューションを使用すると、これを変更できます。

于 2016-10-12T15:20:55.013 に答える