gpt4 book ai didi

ios - 关闭键盘时,如何将现有文本字段保留在同一位置?

转载 作者:行者123 更新时间:2023-11-29 12:54:33 25 4
gpt4 key购买 nike

所以我有一个包含 UITextFields 和 UILabel 的 UIScrollView,当用户在 UITextField 中单击时,键盘会显示。但我需要一种允许用户关闭键盘的方法,我尝试使用向下滑动手势来执行此操作(如在“消息”中)。我将 Gesture Recognizer 添加到 Storyboard 中的 View ,并将 Action 添加到头文件。方法如下:

- (IBAction)SwipeKeyboardDown:(id)sender
{
if(self.moveTextField)
{
[self.moveTextField resignFirstResponder];
}
}

当用户单击第一个文本字段时,我只有 if 用于测试,当他们尝试向下滑动时,所有文本字段和标签都会移动到屏幕底部。我需要做些什么来将文本字段和标签保留在它们的位置吗?我正在使用自动布局,我在 Storyboard 中创建了它们。

这是在向下滑动键盘之前我的 View 的样子: How it looks before swiping keyboard down

这是向下滑动后的样子: enter image description here

感谢您的帮助,如果需要更多信息,请询问,我可以提供更多。

回溯:

2014-02-02 01:28:33.453 BJJDrillingAppPro[512:70b] (
0 BJJDrillingAppPro 0x0001de82 -[AddMoveViewControllerPro observeValueForKeyPath:ofObject:change:context:] + 210
1 Foundation 0x0116e8c7 NSKeyValueNotifyObserver + 362
2 Foundation 0x01170206 NSKeyValueDidChange + 458
3 Foundation 0x0112c8dd -[NSObject(NSKeyValueObserverNotification) didChangeValueForKey:] + 120
4 Foundation 0x010ffcc7 _NSSetPointValueAndNotify + 185
5 UIKit 0x00320cae -[UIScrollView(UIScrollViewInternal) _adjustContentOffsetIfNecessary] + 2622
6 UIKit 0x0030384d -[UIScrollView setContentSize:] + 354
7 BJJDrillingAppPro 0x0002023d -[AddMoveViewControllerPro keyboardDidHide] + 493
8 Foundation 0x011f2bf9 __57-[NSNotificationCenter addObserver:selector:name:object:]_block_invoke + 40
9 CoreFoundation 0x017f8524 __CFNOTIFICATIONCENTER_IS_CALLING_OUT_TO_AN_OBSERVER__ + 20
10 CoreFoundation 0x0175000b _CFXNotificationPost + 2859
11 Foundation 0x0112c951 -[NSNotificationCenter postNotificationName:object:userInfo:] + 98
12 UIKit 0x0071d6f5 -[UIInputViewTransition postNotificationsForTransitionStart] + 1004
13 UIKit 0x007137e2 -[UIPeripheralHost(UIKitInternal) executeTransition:] + 592
14 UIKit 0x00715c0e -[UIPeripheralHost(UIKitInternal) setInputViews:animationStyle:] + 1029
15 UIKit 0x00716019 -[UIPeripheralHost(UIKitInternal) setInputViews:animated:] + 72
16 UIKit 0x00716063 -[UIPeripheralHost(UIKitInternal) setInputViews:] + 67
17 UIKit 0x0070d2fa -[UIPeripheralHost(UIKitInternal) _reloadInputViewsForResponder:] + 1453
18 UIKit 0x003e707c -[UIResponder _finishResignFirstResponder] + 163
19 UIKit 0x003e719f -[UIResponder resignFirstResponder] + 265
20 UIKit 0x0096b5f4 -[UITextField resignFirstResponder] + 118
21 BJJDrillingAppPro 0x0001dd70 -[AddMoveViewControllerPro SwipeKeyboardDown:] + 288
22 UIKit 0x00605e8c _UIGestureRecognizerSendActions + 230
23 UIKit 0x00604b00 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 383
24 UIKit 0x0060656d -[UIGestureRecognizer _delayedUpdateGesture] + 60
25 UIKit 0x00609acd ___UIGestureRecognizerUpdate_block_invoke + 57
26 UIKit 0x00609a4e _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 317
27 UIKit 0x00600148 _UIGestureRecognizerUpdate + 199
28 UIKit 0x002cc19a -[UIWindow _sendGesturesForEvent:] + 1291
29 UIKit 0x002cd0ba -[UIWindow sendEvent:] + 1030
30 UIKit 0x002a0e86 -[UIApplication sendEvent:] + 242
31 UIKit 0x0028b18f _UIApplicationHandleEventQueue + 11421
32 CoreFoundation 0x0172583f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
33 CoreFoundation 0x017251cb __CFRunLoopDoSources0 + 235
34 CoreFoundation 0x0174229e __CFRunLoopRun + 910
35 CoreFoundation 0x01741ac3 CFRunLoopRunSpecific + 467
36 CoreFoundation 0x017418db CFRunLoopRunInMode + 123
37 GraphicsServices 0x03aa39e2 GSEventRunModal + 192
38 GraphicsServices 0x03aa3809 GSEventRun + 104
39 UIKit 0x0028dd3b UIApplicationMain + 1225
40 BJJDrillingAppPro 0x0001bf0d main + 141
41 libdyld.dylib 0x0213c70d start + 1
42 ??? 0x00000001 0x0 + 1
)

在查看我的键盘代码显示和隐藏方法后,我想知道我是否在那里做错了什么:

这是在我的 viewDidLoad 中:

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidShow)
name:UIKeyboardWillShowNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardDidHide)
name:UIKeyboardWillHideNotification
object:nil];

这些是选择器:

- (void)keyboardDidHide
{
if(keyboardShown)
{
CGRect r = self.scrollView.frame;
r.size.height += 216;
self.scrollView.frame = r;

keyboardShown = NO;
}
}

- (void)keyboardDidShow
{
if(!keyboardShown)
{
CGRect r = self.scrollView.frame;
self.scrollView.contentSize = self.scrollView.frame.size;
r.size.height -= 216;
self.scrollView.frame = r;

keyboardShown = YES;
}
}

最佳答案

您不需要手势识别器。

ScrollView 具有 keyboardDismissMode 属性。将其设置为 UIScrollViewKeyboardDismissModeOnDragUIScrollViewKeyboardDismissModeInteractive,您就可以开始了。

关于ios - 关闭键盘时,如何将现有文本字段保留在同一位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21490012/

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