我有一个视图,我在滚动视图中添加了文本字段。滚动很好但是当我想在任何文本字段中写入文本后重新签名键盘然后我无法做到这一点!我需要在视图中的任何位置点击键盘,除了文本字段。有人建议我写一堆代码:
- (void)keyboardWillShow:(NSNotification *)n { // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScrollView if the keyboard is already shown. This can happen if the user, after fixing editing a UITextField, scrolls the resized UIScrollView to another UITextField and attempts to edit the next UITextField. If we were to resize the UIScrollView again, it would be disastrous. NOTE: The keyboard notification will fire even when the keyboard is already shown. if (keyboardIsShown) { return; } NSDictionary* userInfo = [n userInfo]; // get the size of the keyboard NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; CGSize keyboardSize = [boundsValue CGRectValue].size; // resize the noteView CGRect viewFrame = self.scroll.frame; // I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView. viewFrame.size.height -= (keyboardSize.height - kTabBarHeight); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; // The kKeyboardAnimationDuration I am using is 0.3 [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.scroll setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = YES; } - (void)keyboardWillHide:(NSNotification *)n { NSDictionary* userInfo = [n userInfo]; // get the size of the keyboard NSValue* boundsValue = [userInfo objectForKey:UIKeyboardBoundsUserInfoKey]; CGSize keyboardSize = [boundsValue CGRectValue].size; // resize the scrollview CGRect viewFrame = self.scroll.frame; /*I'm also subtracting a constant kTabBarHeight because my UIScrollView was offset by the UITabBar so really only the portion of the keyboard that is leftover pass the UITabBar is obscuring my UIScrollView.*/ viewFrame.size.height += (keyboardSize.height - kTabBarHeight); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; // The kKeyboardAnimationDuration I am using is 0.3 [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.scroll setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = NO; }
我想它不会提供我想要的东西。帮助PLZ
答案 0 :(得分:1)
您需要抓住其他视图的触摸事件(文本字段除外),您可以通过实施touchesBegan
的{{1}}方法来实现此目的。
UIResponder
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
答案 1 :(得分:0)
请尝试下面的代码
- (IBAction)backgroundTap:(id)sender
{
[yourtextfield resignFirstResponder];
}
在XIB中,使用UIView的Touch Down事件附加此方法。
答案 2 :(得分:0)
你能做的一件简单的事情是, 有一个覆盖整个视图的UIButton,并将该按钮连接到@ dks1725提到的方法。
答案 3 :(得分:0)
我发现的最有效(也是代码最少)的方法是here。简而言之,您只需将endEditing:TRUE
发送到包含文本字段的超级视图,并且所有第一响应者状态都会根据其中的任何字段进行重新签名。
答案 4 :(得分:-1)
您有2个选项
在viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
在dismissKeyboard:
函数
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
(其中aTextField是负责键盘的文本字段)
选项2
如果您无法承担添加gestureRecognizer的费用,那么您可以试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
所有积分都转到this answer。
希望它有所帮助。