gpt4 book ai didi

ios - 出现键盘时如何移动 View

转载 作者:行者123 更新时间:2023-11-28 18:32:48 24 4
gpt4 key购买 nike

我有多个 UITextFields,我需要在键盘出现时向上移动 View 。但我只需要 3 个底部字段,并且不想移动顶部其他字段的 View 。我使用这段代码,但是当用户点击每个字段时它会移动 View ,而我只需要在用户点击 2 个底部字段时移动 View

- (void)viewWillAppear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
}

- (void)viewWillDisappear:(BOOL)animated {
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
}

#pragma mark - keyboard movements
- (void)keyboardWillShow:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = -115.0f; //set the -35.0f to your required value
self.view.frame = f;
}];
}

-(void)keyboardWillHide:(NSNotification *)notification
{
[UIView animateWithDuration:0.3 animations:^{
CGRect f = self.view.frame;
f.origin.y = 0.0f;
self.view.frame = f;
}];
}

最佳答案

您可以更改任何 UIView、UIButton 的框架....在 UITextfield 委托(delegate)方法 textFieldDidBeginEditing 中:当您点击文本字段时,该委托(delegate)将调用

 - (void)textFieldDidBeginEditing:(UITextField *)textField{ 

//here you will get the selected textfield
//check whether the textfield is equal to any of three bottom field
if(textfield==BottomTextfield)
{
//here you can change the frame of the UIViews
}
}

关于ios - 出现键盘时如何移动 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24903410/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com