【问题标题】:resize UIView when showing the keyboard for iphone, how to? [duplicate]显示 iphone 的键盘时调整 UIView 的大小,如何? [复制]
【发布时间】:2011-07-21 11:58:53
【问题描述】:

我将向您展示一个众所周知的 whatsapp 示例 当您触摸文本内部时,键盘会弹出,所以我必须向上移动或移动所有条并将视图大小调整为一半,这样我仍然可以看到我正在输入的文本和发送按钮

第一阶段: http://www.appbank.net/wp-content/uploads/2010/10/WhatsAppMessenger-18.jpg

第二阶段: http://www.onetooneglobal.com/wp-content/uploads/2011/02/onetoone_whatsapp_2.png

实现这一目标的最佳方法是什么?

【问题讨论】:

    标签: iphone uiview keyboard resize shift


    【解决方案1】:
    
    
    #define kOFFSET_FOR_KEYBOARD 280.0
    
    - (void)keyboardWillHide:(NSNotification *)notif {
        [self setViewMoveUp:NO];
    }
    
    
    - (void)keyboardWillShow:(NSNotification *)notif{
        [self setViewMoveUp:YES];
    }
    
    
    - (void)textFieldDidBeginEditing:(UITextField *)textField {
        stayup = YES;
        [self setViewMoveUp:YES];
    }
    
    
    - (void)textFieldDidEndEditing:(UITextField *)textField {
        stayup = NO;
        [self setViewMoveUp:NO];
    }
    
    //method to move the view up/down whenever the keyboard is shown/dismissed
    -(void)setViewMoveUp:(BOOL)moveUp
    {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration:0.3]; // if you want to slide up the view
        [UIView setAnimationBeginsFromCurrentState:YES];
    
        CGRect rect = self.view.frame;
        if (moveUp)
        {
            // 1. move the view's origin up so that the text field that will be hidden come above the keyboard 
            // 2. increase the size of the view so that the area behind the keyboard is covered up.
    
            if (rect.origin.y == 0 ) {
                rect.origin.y -= kOFFSET_FOR_KEYBOARD;
                //rect.size.height += kOFFSET_FOR_KEYBOARD;
            }
    
        }
        else
        {
            if (stayup == NO) {
                rect.origin.y += kOFFSET_FOR_KEYBOARD;
                //rect.size.height -= kOFFSET_FOR_KEYBOARD;
            }
        }
        self.view.frame = rect; 
        [UIView commitAnimations];
    }
    
    

    试试这个方法。根据您的要求对其进行编辑。

    【讨论】:

    • 好的,我使用了你的代码并且它工作了,:) 我不得不代替 void , IBAction 并将事件分配给 textedit,我仍然不知道 keyboardWillHide 和 keyboardWillShow 的作用和如何使用它们,..,隐藏我使用的键盘stackoverflow.com/questions/2586937/…。感谢您提供这个出色的解决方案
    • 在某些情况下,如果我们使用 textFieldDidBeginEditing 和 textFieldDidEndEditing,Animation 计时可能并不完美,因此我们可以使用 keyboardWillShow 和 keyboardWillHide 来相应地调整该计时。这两种方法触发得有点早。无论如何,在您的情况下,我认为 textFieldDidBeginEditing 和 textFieldDidEndEditing 就足够了。 :)
    • 熬夜=是;什么是留在这里?
    • 有些键盘的尺寸不同(其中一个是日语输入)。要以正确的方式执行此操作,您需要从通知字典中的 UIKeyboardFrameEndUserInfoKey 值获取键盘边界。
    • 非常感谢。它工作得很好:)
    【解决方案2】:

    您会想要收听UIKeyboardDidShowNotificationUIKeyboardDidHideNotification,并在与您提供通知中心的选择器对应的方法中随意调整视图大小(通常通过更改 UIView.frame 属性)

    【讨论】:

    猜你喜欢
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-20
    • 2021-02-19
    相关资源
    最近更新 更多