我有多个UITextFields,我需要在键盘出现时向上移动视图.但我只需要3个底部字段,并且不希望移动顶部其他字段的视图.我使用这个代码,但是当用户点击每个字段时它的移动视图,而我只需要在用户点击2个底部字段时移动视图
- (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;
}];
}
Run Code Online (Sandbox Code Playgroud)
您可以在UITextfield委托方法中更改任何UIViews,UIButton ....的框架textFieldDidBeginEditing:当您点击文本字段时,此委托将调用
- (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
}
}
Run Code Online (Sandbox Code Playgroud)