编辑文本字段时偏移滚动视图?
Posted
技术标签:
【中文标题】编辑文本字段时偏移滚动视图?【英文标题】:Offsetting a Scroll View when a textfield is being edited? 【发布时间】:2017-04-05 15:09:20 【问题描述】:我目前正在尝试让滚动视图在键盘弹出时偏移。但是,当我开始编辑我设置为委托的文本字段时,它会使应用程序崩溃。任何帮助表示赞赏。
class ViewController: UIViewController, UITextFieldDelegate
@IBOutlet weak var scroll: UIScrollView!
@IBOutlet weak var bottomText: UITextField!
override func viewDidLoad()
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
override func didReceiveMemoryWarning()
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
func textFieldDidBeginEditing(_ textField: UITextField)
//scroll.setContentOffset(CGPoint(x: 0,y: 250), animated: true)
print("test")
这是日志的截图。
这是完整的堆栈跟踪
(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = breakpoint 1.1
* frame #0: 0x0000000105efb237 TestingScrollView`ViewController.textFieldDidBeginEditing(textField=0x00007fcf807065e0, self=0x00007fcf8040de90) -> () at ViewController.swift:27
frame #1: 0x0000000105efb31a TestingScrollView`@objc ViewController.textFieldDidBeginEditing(UITextField) -> () at ViewController.swift:0
frame #2: 0x0000000107d40970 UIKit`-[UITextField _becomeFirstResponder] + 641
frame #3: 0x0000000107457e21 UIKit`-[UIResponder becomeFirstResponder] + 436
frame #4: 0x00000001072f12f7 UIKit`-[UIView(Hierarchy) becomeFirstResponder] + 137
frame #5: 0x0000000107d3f956 UIKit`-[UITextField becomeFirstResponder] + 50
frame #6: 0x00000001077856d2 UIKit`-[UITextInteractionAssistant(UITextInteractionAssistant_Internal) setFirstResponderIfNecessary] + 208
frame #7: 0x0000000107788e28 UIKit`-[UITextInteractionAssistant(UITextInteractionAssistant_Internal) oneFingerTap:] + 3772
frame #8: 0x0000000107776c09 UIKit`-[UIGestureRecognizerTarget _sendActionWithGestureRecognizer:] + 57
frame #9: 0x000000010777ea07 UIKit`_UIGestureRecognizerSendTargetActions + 109
frame #10: 0x000000010777c3bb UIKit`_UIGestureRecognizerSendActions + 225
frame #11: 0x000000010777b67e UIKit`-[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 981
frame #12: 0x0000000107767e02 UIKit`_UIGestureEnvironmentUpdate + 1219
frame #13: 0x00000001077678f3 UIKit`-[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 484
frame #14: 0x0000000107766aba UIKit`-[UIGestureEnvironment _updateGesturesForEvent:window:] + 274
frame #15: 0x00000001072b2b9a UIKit`-[UIWindow sendEvent:] + 4092
frame #16: 0x000000010725f7b0 UIKit`-[UIApplication sendEvent:] + 352
frame #17: 0x0000000107a42adc UIKit`__dispatchPreprocessedEventFromEventQueue + 2926
frame #18: 0x0000000107a3aa3a UIKit`__handleEventQueue + 1122
frame #19: 0x0000000109263c01 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
frame #20: 0x00000001092490cf CoreFoundation`__CFRunLoopDoSources0 + 527
frame #21: 0x00000001092485ff CoreFoundation`__CFRunLoopRun + 911
frame #22: 0x0000000109248016 CoreFoundation`CFRunLoopRunSpecific + 406
frame #23: 0x000000010b1c8a24 GraphicsServices`GSEventRunModal + 62
frame #24: 0x00000001072420d4 UIKit`UIApplicationMain + 159
frame #25: 0x0000000105efc797 TestingScrollView`main at AppDelegate.swift:12
frame #26: 0x000000010a25d65d libdyld.dylib`start + 1
frame #27: 0x000000010a25d65d libdyld.dylib`start + 1
(lldb)
这是错误截图
【问题讨论】:
你检查过滚动的值吗?崩溃在控制台中记录的堆栈跟踪是什么? @Pacu 我添加了日志的截图。检查滚动的值是什么意思?我最初确实让它的高度为 1000,但删除了它,因为我假设我不应该特别需要它来完成这个简单的任务。 我的意思是在调试器上。此外,粘贴所描述的崩溃的堆栈跟踪也很重要。 90% 的时候它是解决问题的线索 我已经粘贴了完整的堆栈跟踪(希望我对这个术语和 Xcode 很陌生)。此外,我通过调试器查看了日志,但我并不完全确定我在寻找什么来检查滚动的值。它说它是活跃的并且有几个限制。 重现崩溃并粘贴该堆栈跟踪。 【参考方案1】:糟糕 - 应用程序在意外设置的断点处停止。容易做到:
⌘+\
将在当前行设置(实际上是切换)断点。
【讨论】:
以上是关于编辑文本字段时偏移滚动视图?的主要内容,如果未能解决你的问题,请参考以下文章
调整keyboardWillBeShown/keyboardWillBeHidden的内容时如何防止滚动视图弹跳