NavController 中的滚动视图在显示键盘后不会重置(Swift)

Posted

技术标签:

【中文标题】NavController 中的滚动视图在显示键盘后不会重置(Swift)【英文标题】:scrollView in NavController will not reset after displaying keyboard (Swift) 【发布时间】:2015-07-28 03:17:11 【问题描述】:

我曾尝试按照此处的其他指南进行操作,但无济于事。 我的应用程序结构目前是一个导航控制器,它通向嵌入了滚动视图的视图控制器。文本字段、文本视图、按钮等位于滚动视图的顶部。当键盘出现时,我通过点击外部将其关闭,滚动视图会随着键盘向上移动,但不会向下移动。看起来这个问题是由导航控制器或导航栏引起的。我该如何解决?

编辑:我刚刚意识到,每次我模拟那个特定的视图控制器时,在我点击任何 textField/textView 之前,其中的所有内容都会向下移动相同的距离。然后当键盘显示出来时,它只是向上移动它,所以它看起来就像它在情节提要中的设置方式。如何摆脱最初的向下位移?

@IBOutlet var scrollView: UIScrollView!
var activeTextView:UIView? = UIView()
@IBOutlet var main: UITextView!
@IBOutlet var initials: UITextField!
@IBOutlet var maleWord: UITextField!
@IBOutlet var maleButton: UIButton!
@IBOutlet var femaleWord: UITextField!
@IBOutlet var femaleButton: UIButton!
@IBOutlet var age: UITextField!


func registerForKeyboardNotifications() 
    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeShown:",
        name: UIKeyboardWillShowNotification,
        object: nil)
    notificationCenter.addObserver(self,
        selector: "keyboardWillBeHidden:",
        name: UIKeyboardWillHideNotification,
        object: nil)



func tapped() 
    initials.resignFirstResponder()
    main.resignFirstResponder()
    age.resignFirstResponder()

// Called when the UIKeyboardDidShowNotification is sent.
func keyboardWillBeShown(sender: NSNotification) 
    let info: NSDictionary = sender.userInfo!
    let value: NSValue = info.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let keyboardSize: CGSize = value.CGRectValue().size

    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
    scrollView.contentInset = contentInsets
    scrollView.scrollIndicatorInsets = contentInsets

    // If active text field is hidden by keyboard, scroll it so it's visible
    var aRect: CGRect = self.view.frame
    aRect.size.height -= keyboardSize.height
    let activeTextViewRect: CGRect? = self.main.frame
    let activeTextViewOrigin: CGPoint? = activeTextViewRect?.origin
    if (!CGRectContainsPoint(aRect, activeTextViewOrigin!)) 
        scrollView.scrollRectToVisible(activeTextViewRect!, animated:true)

    



// Called when the UIKeyboardWillHideNotification is sent
func keyboardWillBeHidden(sender: NSNotification) 

    self.scrollView .setContentOffset(CGPointMake(0, 0), animated: true)
    self .viewDidLayoutSubviews()
    self.activeTextView = nil


func textViewDidBeginEditing(textView: UITextView) 

self.activeTextView = textView
scrollView.scrollEnabled = true


func textViewDidEndEditing(textView: UITextView) 

self.activeTextView = nil
    scrollView.scrollEnabled = false
    self.scrollView .setContentOffset(CGPointMake(0, 0), animated: true)


func textFieldShouldReturn(textField: UITextField) -> Bool // called when 'return' key pressed. return NO to ignore.

    textField.resignFirstResponder()
    return true;


override func viewDidLoad() 
    super.viewDidLoad()
    var tap = UITapGestureRecognizer (target: self, action: ("tapped"))
    self.view.addGestureRecognizer(tap)
    self.main.delegate = self
    self.initials.delegate = self
    self.age.delegate = self
 self.registerForKeyboardNotifications()


【问题讨论】:

【参考方案1】:

更新您的函数,如下所示

func keyboardWillBeHidden(sender: NSNotification) 

    let contentInsets: UIEdgeInsets = UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0)
    scrollView.contentInset = contentInsets
    self.scrollView .setContentOffset(CGPointMake(0, 0), animated: true)
    self .viewDidLayoutSubviews()
    self.activeTextView = nil

【讨论】:

仍然卡住..它只用键盘向上滚动而不会向下滚动。谢谢你 如果您为特定的视图控制器设置了 Under Top Bars 属性,请检查情节提要。尝试取消设置并再次检查。取消设置时,您可能需要重置滚动视图的框架。

以上是关于NavController 中的滚动视图在显示键盘后不会重置(Swift)的主要内容,如果未能解决你的问题,请参考以下文章

我设置滚动视图偏移以显示被键盘隐藏的文本字段。如果用户在显示键盘时滚动,则滚动视图会向下捕捉

UIScrollView 仅在显示键盘时才需要

Swift:当键盘显示时向上滚动视图

在 navcontroller 中的 tabbarcontroller 上方显示 top viewcontroller

滚动到滚动视图/滚动视图中的特定文本字段 当键盘在屏幕上时停止滚动

显示键盘时出现滚动 UITableViewController