显示 iPhone 键盘时调整 UIView 大小,如何操作?

发布于 2024-10-25 17:26:40 字数 526 浏览 1 评论 0原文

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

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

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

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

I will show you an example with the well known whatsapp
When you touch inside the text the keyboard pops up , so I have to move or shift all that bar up and resize the view to half, so I can still see the text that I'm typing and the send button

Phase 1:
http://www.appbank.net/wp-content/uploads/2010/10/WhatsAppMessenger-18.jpg

Phase 2:
http://www.onetooneglobal.com/wp-content/uploads/2011/02/onetoone_whatsapp_2.png

What would be the best way to achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

物价感观 2024-11-01 17:26:56


#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];
}

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



#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];
}

Try this methods. Edit it according to your requirement.

时光是把杀猪刀 2024-11-01 17:26:56

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

You'll want to listen to the UIKeyboardDidShowNotification and UIKeyboardDidHideNotification, and in the method that corresponds to the selector you've provided the notification center resize your views at will (typically by changing the UIView.frame property)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文