键盘上方的本机脚本移动按钮

时间:2018-11-19 14:13:16

标签: angular2-nativescript nativescript-angular

如何将打开的键盘覆盖的按钮移到键盘上方,并在关闭键盘时将其移回?

2 个答案:

答案 0 :(得分:0)

您可以使用nativescript-IQKeyboardManager插件。可以解决ios的上述情况。 要使用该插件,您只需要安装它即可。

tns plugin add nativescript-iqkeyboardmanager

P.S。以上解决方案适用于IOS。对于Android,您需要在App_Resources / Android / src / main / AndroidManifest.xml

中将adjustResize修复为windowSoftInputMode
<activity>
        android:name="com.tns.NativeScriptActivity"
        android:windowSoftInputMode="adjustResize"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:theme="@style/LaunchScreenTheme">
</activity>

如果您希望文本框和键盘之间有更大的距离

const iqKeyboard = IQKeyboardManager.sharedManager();
iqKeyboard.overrideKeyboardAppearance = true;
iqKeyboard.keyboardAppearance = UIKeyboardAppearance.Dark;
iqKeyboard.keyboardDistanceFromTextField = 40.0

答案 1 :(得分:0)

您可以尝试:https://gist.github.com/elvticc/0c789d08d57b1f4d9273f7d93a7083ec

// Also use IQKeyboardManager to customize the iOS keyboard
// See https://github.com/tjvantoll/nativescript-IQKeyboardManager

// let iqKeyboard: IQKeyboardManager = IQKeyboardManager.sharedManager();
// iqKeyboard.toolbarDoneBarButtonItemText             = "OK";
// iqKeyboard.canAdjustAdditionalSafeAreaInsets        = true;
// iqKeyboard.shouldFixInteractivePopGestureRecognizer = true;

// Angular
[...]
import { OnInit, OnDestroy, ElementRef, ViewChild } from "@angular/core";
[...]

// NativeScript
[...]
import { ios as iosApp } from "tns-core-modules/application/application";
[...]

@ViewChild("element") private _element: ElementRef<StackLayout>; // FlexboxLayout, GridLayout, etc.

private _keyboardHeight: number = 0;
private _elementHeight:  number = 0;
private _observerIDs:    Array<object> = new Array();

// Start events when the component is ready
ngOnInit(): void {

    // iOS keyboard events
    if (iosApp) {

        let eventNames: Array<string> = [
                UIKeyboardWillShowNotification,
                UIKeyboardDidShowNotification,
                UIKeyboardWillHideNotification
            ];

        // Catch the keyboard height before it appears
        this._observerIDs.push({
            event: eventNames[0],
            id: iosApp.addNotificationObserver(eventNames[0], (event) => {

                    let currHeight: number = this._keyboardHeight,
                        newHeight:  number = event.userInfo.valueForKey(UIKeyboardFrameEndUserInfoKey).CGRectValue.size.height;

                    if (currHeight != newHeight) {

                        this._keyboardHeight = newHeight;

                    }

                })
        });

        // Position the element according to the height of the keyboard
        this._observerIDs.push({
            event: eventNames[1],
            id: iosApp.addNotificationObserver(eventNames[1], (event) => {

                    if (this._elementHeight == 0) {

                        this._elementHeight = this._element.nativeElement.getActualSize().height;

                    }

                    this._element.nativeElement.height = this._keyboardHeight + this._elementHeight;

                })
        });

        // Reposition the element according to its starting height
        this._observerIDs.push({
            event: eventNames[2],
            id: iosApp.addNotificationObserver(eventNames[2], () => {

                    this._element.nativeElement.height = this._elementHeight; // or "auto";

                })
        });

    }

}

// Stop events to avoid a memory leak
ngOnDestroy(): void {

    if (iosApp) {

        let index: number = 0;

        for (index; index < this._observerIDs.length; index++) {

            let observerId: number = this._observerIDs[index]['id'],
                eventName:  string = this._observerIDs[index]['event'];

            iosApp.removeNotificationObserver(observerId, eventName);

        }

    }

}

Marcel Ploch的原著:https://gist.github.com/marcel-ploch/bf914dd62355049a0e5efb4885ca4c6e