質問

仕事をしていiPadアプリを使用3.2ンポーネント。取り扱って取得するキーボードのサイズを防ぐっtextfieldsからhiddingます。

もしかしたら警告を発するXcode->UIKeyboardBoundsUserInfoKeyが推奨されていませんどうした代わりに使いこと警告?

役に立ちましたか?

解決

私は、以前提供溶液で演奏が、まだ問題がありました。ここで私が代わりに思い付いたものです。

    - (void)keyboardWillShow:(NSNotification *)aNotification {
    [self moveTextViewForKeyboard:aNotification up:YES];
}

    - (void)keyboardWillHide:(NSNotification *)aNotification {
        [self moveTextViewForKeyboard:aNotification up:NO]; 
    }

- (void) moveTextViewForKeyboard:(NSNotification*)aNotification up: (BOOL) up{
NSDictionary* userInfo = [aNotification userInfo];

// Get animation info from userInfo
NSTimeInterval animationDuration;
UIViewAnimationCurve animationCurve;

CGRect keyboardEndFrame;

[[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
[[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];


[[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame];


// Animate up or down
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:animationDuration];
[UIView setAnimationCurve:animationCurve];

CGRect newFrame = textView.frame;
CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil];

newFrame.origin.y -= keyboardFrame.size.height * (up? 1 : -1);
textView.frame = newFrame;

[UIView commitAnimations];
}

他のヒント

から 文書 のための UIKeyboardBoundsUserInfoKey:

の鍵となるNSValueオブジェクトを含むCGRectを識別する境界の矩形のあのキーボード画面座標です。この値は十分に得のサイズをする必要があります。取得したい場合は、元のキーボードの画面の前後アニメーション)をご利用価値から取得したユーザー情報の辞書のUIKeyboardCenterBeginUserInfoKeyはUIKeyboardCenterEndUserInfoKey定数. 利用のUIKeyboardFrameBeginUserInfoKeyはUIKeyboardFrameEndUserInfoKeyキーです。

Apple勧告の実施に便利な日常的なこと(実施可能としてカテゴリーのほか、 UIScreen):

+ (CGRect) convertRect:(CGRect)rect toView:(UIView *)view {
    UIWindow *window = [view isKindOfClass:[UIWindow class]] ? (UIWindow *) view : [view window];
    return [view convertRect:[window convertRect:rect fromWindow:nil] fromView:nil];
}

回復ウィンドウを調整キーボードフレームサイズの物件です。

また異なるアプローチ、チェックのデバイス方向:

CGRect _keyboardEndFrame;
[[notification.userInfo valueForKey:UIKeyboardFrameEndUserInfoKey] getValue:&_keyboardEndFrame];
CGFloat _keyboardHeight = ([[UIDevice currentDevice] orientation] == UIDeviceOrientationPortrait || [[UIDevice currentDevice] orientation] == UIDeviceOrientationPortraitUpsideDown) ? _keyboardEndFrame.size.height : _keyboardEndFrame.size.width;

あなたは、単にこのコードを使用します

//NSVale *aValue = [info objectForKey:UIKeyboardBoundsUserInfoKey];
//instead of Upper line we can use either next line or nextest line.
//NSValue *aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey];
NSValue *aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

次のコードする問題を修正に ジェイからの回答, は UIKeyboardWillShowNotification な火災時にもスキーボードがすでに存在する。

タイピングを日本語/中国語のキーボードミュレーションソフトウェアを火災に追加 UIKeyboardWillShowNotification 新しいキーボードフレームのもののキーボードが現在の高さ self.textView 軽減される第二の時間のコードです。

この削減 self.textView ほとんど何もなし。そのことができなくな回復からこの問題以降、また、シングルル UIKeyboardWillHideNotification 次回のキーボードが解雇された。

代わりに引の追加高 self.textView かどうかに応じてキーボードが表示/非表示として、独自のコードでは、以下のコードで計算可能な限り最大の高さ self.textView を差し引いた後の高さにキーボードの画面になります。

self.textView あるいは全体をコントローラーとしては、いくことになるかもしれません。subviewるニーズが見えることになります。

- (void)resizeTextViewWithKeyboardNotification:(NSNotification*)notif {

    NSDictionary* userInfo = [notif userInfo];
    NSTimeInterval animationDuration;
    UIViewAnimationCurve animationCurve;
    CGRect keyboardFrameInWindowsCoordinates;

    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrameInWindowsCoordinates];

    [self resizeTextViewToAccommodateKeyboardFrame:keyboardFrameInWindowsCoordinates
                             withAnimationDuration:animationDuration
                                    animationCurve:animationCurve];

}

- (void)resizeTextViewToAccommodateKeyboardFrame:(CGRect)keyboardFrameInWindowsCoordinates
                           withAnimationDuration:(NSTimeInterval)duration
                                  animationCurve:(UIViewAnimationCurve)curve
{

    CGRect fullFrame = self.view.frame;

    CGRect keyboardFrameInViewCoordinates =
    [self.view convertRect:keyboardFrameInWindowsCoordinates fromView:nil];

    // Frame of the keyboard that intersects with the view. When keyboard is
    // dismissed, the keyboard frame still has width/height, although the origin
    // keeps the keyboard out of the screen.
    CGRect keyboardFrameVisibleOnScreen =
    CGRectIntersection(fullFrame, keyboardFrameInViewCoordinates);

    // Max frame availble for text view. Assign it to the full frame first
    CGRect newTextViewFrame = fullFrame;

    // Deduct the the height of any keyboard that's visible on screen from
    // the height of the text view
    newTextViewFrame.size.height -= keyboardFrameVisibleOnScreen.size.height;

    if (duration)
    {
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:duration];
        [UIView setAnimationCurve:curve];
    }

    // Adjust the size of the text view to the new one
    self.textView.frame = newTextViewFrame;

    if (duration)
    {
        [UIView commitAnimations];
    }

}

また、忘れずに登録のキーボードの通知viewDidLoad:

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSNotificationCenter* notifCenter = [NSNotificationCenter defaultCenter];

    [notifCenter addObserver:self selector:@selector(resizeTextViewWithKeyboardNotification:) name:UIKeyboardWillShowNotification object:nil];
    [notifCenter addObserver:self selector:@selector(resizeTextViewWithKeyboardNotification:) name:UIKeyboardWillHideNotification object:nil];
}

約分のサイズ変更コード部

そのtextViewサイズ変更コード間(resizeTextViewWithKeyboardNotification:resizeViewToAccommodateKeyboardFrame:withAnimationDuration:animationCurve: は直別される際に問題が起きた場合、キーボードの継続を通じてプッシュからビューコントローラを他の参照 方法を検出するiOSキーボードでの滞在とコントローラ?).

以降のキーボードがすでに存在する前にビューコントローラーを押して、追加のキーボードの通知の発iOSは、このようなサイズの変更の textView それにもとづくキーボードを通知を選択するフィルタ。

上記のコード(独自のコード)のサイズ self.textView まれた場合にのみ、キーボードを表示 ビューにて読み込まれます。

私の溶液をシングルトンを格納する最後のキーボード座標、 - viewDidAppear: のviewController話:

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // Resize the view if there's any keyboard presence before this
    // Only call in viewDidAppear as we are unable to convertRect properly
    // before view is shown
    [self resizeViewToAccommodateKeyboardFrame:[[UASKeyboard sharedKeyboard] keyboardFrame]
                         withAnimationDuration:0
                                animationCurve:0];
}

UASKeyboard 私はシングルトンです。理想的にはお呼び出す必要があるこ - viewWillAppear:, しかし、私の経験(少なくともiOS6)、 convertRect:fromView: 方法としての利用 resizeViewToAccommodateKeyboardFrame:withAnimationDuration:animationCurve: なのに適切に変換できませんのキーボードフレーム、ビュー座標が以前のビューが可視にします。

だけではなく、UIKeyboardFrameBeginUserInfoKeyUIKeyboardFrameEndUserInfoKeyまたはUIKeyboardBoundsUserInfoKeyキーを使用する

@Jason、あなたのコードであれば一点を除いて細かいます。

現時点では、あなたが実際に何かをアニメーション化されていないと、ビューは単に `その新しいsize.height」にポップアップ表示されます。

あなたは、そこからアニメーションまで状態を指定することがあります。アニメーションは(状態から)の一種である - 。>(状態)のこと。

幸い(状態から)のようなビューの現在の状態を指定するための非常に便利な方法があります。

[UIView setAnimationBeginsFromCurrentState:YES];
あなたは右beginAnimations後にその行を追加する場合は、:コンテキスト:あなたのコードは完璧に動作します。

- (CGSize)keyboardSize:(NSNotification *)aNotification {
    NSDictionary *info = [aNotification userInfo];
    NSValue *beginValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey];

    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];

    CGSize keyboardSize;
    if ([UIKeyboardDidShowNotification isEqualToString:[aNotification name]]) {
        _screenOrientation = orientation;
        if (UIDeviceOrientationIsPortrait(orientation)) {
            keyboardSize = [beginValue CGRectValue].size;
        } else {
            keyboardSize.height = [beginValue CGRectValue].size.width;
            keyboardSize.width = [beginValue CGRectValue].size.height;
        }
    } else if ([UIKeyboardDidHideNotification isEqualToString:[aNotification name]]) {
        // We didn't rotate
        if (_screenOrientation == orientation) {
            if (UIDeviceOrientationIsPortrait(orientation)) {
                keyboardSize = [beginValue CGRectValue].size;
            } else {
                keyboardSize.height = [beginValue CGRectValue].size.width;
                keyboardSize.width = [beginValue CGRectValue].size.height;
            }
        // We rotated
        } else if (UIDeviceOrientationIsPortrait(orientation)) {
            keyboardSize.height = [beginValue CGRectValue].size.width;
            keyboardSize.width = [beginValue CGRectValue].size.height;
        } else {
            keyboardSize = [beginValue CGRectValue].size;
        }
    }


    return keyboardSize;
}

そのは次のように働いていた。

このは保存ボタンの下の制約である。

@IBOutlet weak var saveBtnBottom: NSLayoutConstraint!
@IBOutlet weak var nameText: UITextField!

内のviewDidLoad

NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillShow(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyBoardWillHide(notification:)), name: UIResponder.keyboardWillHideNotification, object: nil)
nameText.delegate = self

これは、我々が必要とする機能である。

func textFieldShouldReturn(_ textField: UITextField) -> Bool {
    nameText.resignFirstResponder()
    return true
}


@objc func keyBoardWillShow(notification: Notification){
    if let userInfo = notification.userInfo as? Dictionary<String, AnyObject>{
        let frame = userInfo[UIResponder.keyboardFrameEndUserInfoKey]
        let keyBoardRect = frame?.cgRectValue
        if let keyBoardHeight = keyBoardRect?.height {
            self.saveBtnBottom.constant = keyBoardHeight 

            UIView.animate(withDuration: 0.5, animations: {
                self.view.layoutIfNeeded()
            })
        }
    }
}

@objc func keyBoardWillHide(notification: Notification){
    self.saveBtnBottom.constant = 30.0
    UIView.animate(withDuration: 0.5, animations: {
        self.view.layoutIfNeeded()
    })
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top