Xamarin iOSでキーボード表示したときにコントロールが隠れないようにするスニペット
Xamarin.iOSでキーボード表示したときにテキストフィールドが隠れないようにアニメするスニペット。
拡張したコードとか含まれてるのでよしなに。
_contentView.KeyboardAnimNeedYはコンテンツの方でテキストフィールの下にこれぐらいの空間あるのでこれと比べて必要だったらアニメしてちょという値です。
戻すときはその値使ってやるのだるかったので自前のライブラリでレイアウトしなおしてるのでこれもよしなに。
public override void ViewWillAppear(bool animated) { base.ViewWillAppear(animated); _showNOb = NSNotificationCenter.DefaultCenter.AddObserver( UIKeyboard.WillShowNotification, n => { var h = ((NSValue)n.UserInfo[UIKeyboard.FrameBeginUserInfoKey]).CGRectValue.Height; var v =((NSNumber) n.UserInfo[UIKeyboard.AnimationDurationUserInfoKey]).DoubleValue; Console.WriteLine(h.GetType().FullName); UIView.Animate(v, () => { var dy =Math.Max(0,h- _contentView.KeyboardAnimNeedY); _contentView.Frame = _contentView.Frame.Slide(0,-(nfloat) dy); }); }); _hideNOb = NSNotificationCenter.DefaultCenter.AddObserver( UIKeyboard.WillHideNotification, n => { var v = ((NSNumber)n.UserInfo[UIKeyboard.AnimationDurationUserInfoKey]).DoubleValue; UIView.Animate(v, () => { _lSet.DoLayout(); }); }); } public override void ViewWillDisappear(bool animated) { base.ViewWillDisappear(animated); NSNotificationCenter.DefaultCenter.RemoveObservers(new[] { _showNOb, _hideNOb }); }