第二个VC中的文本定位

时间:2015-12-22 07:14:29

标签: ios swift uitableview text uitextview

我已设置通知以将textView中的文本保留在键盘上方。我的问题是,当我选择表视图单元格以在下一个VC中显示我的文本时,文本在顶部下方几行显示,并且仅在我开始编辑(键盘显示)之后,而不是滑动以关闭键盘,文本重新定位它本身就在顶部,应该是什么。在键盘出现之前,一旦VC显示其内容,我怎么能强制文本在顶部?感谢。

override func viewDidLoad() {
    super.viewDidLoad()

    let notificationCenter = NSNotificationCenter.defaultCenter()
    notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillHideNotification, object: nil)
    notificationCenter.addObserver(self, selector: "adjustForKeyboard:", name: UIKeyboardWillChangeFrameNotification, object: nil)


    storyTextView.text = passedText
    title = passedTitle

    navigationController?.navigationBar.tintColor = UIColor.redColor()
}

func adjustForKeyboard(notification: NSNotification) {
    let userInfo = notification.userInfo!

    let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
    let keyboardViewEndFrame = view.convertRect(keyboardScreenEndFrame, fromView: view.window)

    if notification.name == UIKeyboardWillHideNotification {
        storyTextView.contentInset = UIEdgeInsetsZero
    }else {
        storyTextView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
    }

    storyTextView.scrollIndicatorInsets = storyTextView.contentInset

    let selectedRange = storyTextView.selectedRange
    storyTextView.scrollRangeToVisible(selectedRange)
}

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    storyTextView.contentInset = UIEdgeInsetsZero
}

0 个答案:

没有答案