Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 单击视图的任何部分时调整键盘的大小_Iphone - Fatal编程技术网

Iphone 单击视图的任何部分时调整键盘的大小

Iphone 单击视图的任何部分时调整键盘的大小,iphone,Iphone,我有一个视图,其中我在滚动视图中添加了文本字段。滚动很好,但当我在任何文本字段中写入文本后想退出键盘时,我就不能这样做!!我需要退出键盘,只要我点击任何地方在视图中除了文本字段。有人建议我写一堆代码: - (void)keyboardWillShow:(NSNotification *)n { // This is an ivar I'm using to ensure that we do not do the frame size adjustment on the UIScroll

我有一个视图,其中我在滚动视图中添加了文本字段。滚动很好,但当我在任何文本字段中写入文本后想退出键盘时,我就不能这样做!!我需要退出键盘,只要我点击任何地方在视图中除了文本字段。有人建议我写一堆代码:

- (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

您有两个选项

在viewDidLoad中

在键盘中:函数

其中aTextField是负责键盘的文本字段

选择2

如果你负担不起添加手势识别器,那么你可以试试这个

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [aTextField resignFirstResponder];
    }
}
所有的功劳都归于他


希望对您有所帮助。

您需要捕获除文本字段之外的其他视图的触摸事件,您可以通过实现UIResponder的touchesbeent方法来实现

-voidTouchesBegined:NSSet*接触事件:UIEvent*事件

试试下面的代码

- (IBAction)backgroundTap:(id)sender 
{ 
     [yourtextfield resignFirstResponder];
}

在XIB中,将此方法与UIView的触地事件连接起来。

您可以做的一件简单的事情是,
有一个覆盖整个视图的UIButton,并将该按钮连接到@dks1725提到的方法。

但是我的视图中有很多文本字段。那么,在任何文本中写入文本后,我如何能够退出键盘呢field@user720235:对于这种情况,有一种方法,使用UITextField ivar保存当前文本字段引用,但我的视图中有这么多文本字段。那么,在任何文本字段中写入文本后,我如何才能退出键盘呢?JST有一个textfield的ivar,并将当前textfield的引用添加到textfieldShouldBeginEditing中的引用。
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) {
        [aTextField resignFirstResponder];
    }
}
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch * touch = [touches anyObject];
    [myTextField resignFirstResponder];
}
- (IBAction)backgroundTap:(id)sender 
{ 
     [yourtextfield resignFirstResponder];
}