在视图下面和在键盘顶部的稠粘的按钮

时间:2016-07-21 15:13:48

标签: ios swift

我想在我的应用中为我的表单添加一个按钮,无论滚动如何都粘贴在视图的底部。如何实现此功能?

Button at the bottom of the view

Button on top of the keyboard

1 个答案:

答案 0 :(得分:0)

您必须将该视图作为主ViewController视图的直接子视图,并将其放置在视图的底部。这可以按照你想要的任何方式完成。然后把它移到"胶水"键盘显示或隐藏时,它会显示在视图的底部,您必须在UIKeyboardWillShowNotification中收听UIKeyboardWillHideNotificationNSNotificationCenter。当这些事件触发时,您可以模仿动画持续时间和缓动方法,并在willshow和willhide函数中向上或向下移动您的底部视图。像这样:

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    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];


    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:animationDuration];
    [UIView setAnimationCurve:animationCurve];
    //Do whatever you would like to here, likely move your bottom view
    [UIView commitAnimations];
}