键盘出现后调整UI大小。尺寸和尺寸问题

时间:2014-06-30 12:32:00

标签: ios ios7 xamarin.ios

我想在键盘出现后调整UIView(例如TextField,UIGridView,...)的大小。获得适当的事件不是问题。我真的通过计算增量来缩小我的视图,这取决于键盘的高度。

如果我以纵向模式将iPad放在我面前,所有计算都会按预期进行。但如果我的设备处于横向模式或颠倒,我真的很难获得正确的尺寸!某些视图框架/边界未转换(在横向模式下,高度仍然高于其宽度)。如果我已经在横向模式下启动了我的应用程序,则会翻译其他视图。下一个挑战是采用一致的规模措施。有时尺寸就像真实的像素(对于新的iPad 2048),有时不考虑比例因子(对于新的iPad 1024)。

任何其他想法如何正确的帧大小?或者是否有任何帮助者识别当前的方向并为我提供有效的尺寸?

我目前实现预期行为的代码是这样的:

MyViewController

public override void ViewWillAppear(bool animated)
{
    base.ViewWillAppear(animated);

    // My code...

    AppDelegate.CurrentDelegate.KeyboardAppeared += KeyboardAppeared;
    AppDelegate.CurrentDelegate.KeyboardDisappeared += KeyboardDisappeared;
}

public override void ViewWillDisappear(bool animated)
{
    base.ViewWillDisappear(animated);

    AppDelegate.CurrentDelegate.KeyboardAppeared -= KeyboardAppeared;
    AppDelegate.CurrentDelegate.KeyboardDisappeared -= KeyboardDisappeared;
}
private void KeyboardAppeared(object sender, KeyboardEventArgs e)
{
    if (_keyboardButton != null)
        _keyboardButton.Enabled = true;

    var frame = LayoutHelper.RectangleForKeyboardAppearance(_textField, View.Window, e.Height);
    LayoutHelper.ResizeViewForKeyboard(_textField, frame);
}

private void KeyboardDisappeared(object sender, EventArgs e)
{
    if (_keyboardButton != null)
        _keyboardButton.Enabled = false;

    var frame = View.Bounds;
    LayoutHelper.ResizeViewForKeyboard(_textField, frame);
}

MyHelperClass

public static RectangleF RectangleForKeyboardAppearance(UIView view, float windowHeight, float keyboardHeight)
{
    var topOfKeyboard = windowHeight - keyboardHeight; // Obere linke Ecke der Tastatur auf den Bildschirm bezogen
    var viewHeightWindow1 = view.ConvertPointFromView(new PointF(0, topOfKeyboard), null);
    var frame = new RectangleF(view.Frame.X, view.Frame.Y, view.Frame.Width, (viewHeightWindow1.Y + view.Bounds.Y));


    return frame;
}

public static void ResizeViewForKeyboard(UIView view, RectangleF frame)
{
    UIView.BeginAnimations("viewMovementForKeyboard");
    UIView.SetAnimationBeginsFromCurrentState(true);
    UIView.SetAnimationDuration(0.3f);
    view.Frame = frame;
    UIView.CommitAnimations();
}

0 个答案:

没有答案