在编辑文本字段时偏移滚动视图?

Offsetting a Scroll View when a textfield is being edited?

我目前正在尝试让滚动视图在键盘弹出时偏移。但是,当我开始编辑我设置为代表的文本字段时,它会使应用程序崩溃。感谢任何帮助。

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) 

这里是错误的截图

糟糕 - 应用程序在意外设置的断点处停止。容易做到:

+\

将在当前行设置(实际上是切换)断点。