简体   繁体   English

键盘在出现时会隐藏UITextView

[英]Keyboard hides UITextView when it appears

I have set my UIkeyboarddismissmode to be .Interactive , and I do not know how to resize the UITextView so that the keyboard would not cover the content. 我已经把我的UIkeyboarddismissmode.Interactive ,我不知道该如何调整UITextView使键盘不会涵盖的内容。 I also have a UIToolbar as the inputaccessory . 我还有一个UIToolbar作为inputaccessory

Here is my code. 这是我的代码。

@IBOutlet weak var textView: UITextView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.addDoneButtonOnKeyboard()

    self.textView.keyboardDismissMode = .Interactive
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    // Add notification about keyboard
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillShowNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardNotification:"), name:UIKeyboardWillHideNotification, object: nil)
    //        NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("keyboardFrameDidChange:"), name:UIKeyboardWillChangeFrameNotification, object: nil)
}

override func viewDidDisappear(animated: Bool) {
    super.viewDidDisappear(true)
    NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillShowNotification)
    NSNotificationCenter.defaultCenter().removeObserver(UIKeyboardWillHideNotification)
}


// The UIToolbar

var toolbar = UIToolbar()

func addDoneButtonOnKeyboard()
{
    var doneToolbar: UIToolbar = UIToolbar(frame: CGRectMake(0, 0, 320, 44))
    doneToolbar.barStyle = UIBarStyle.Default

    var flexSpace = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil)
    var photo: UIBarButtonItem = UIBarButtonItem(title: "Add Photo", style: UIBarButtonItemStyle.Bordered, target: self, action: Selector("photoButtonAction"))
    var done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: self, action: Selector("doneButtonAction"))

    var items = NSMutableArray()
    items.addObject(photo)
    items.addObject(flexSpace)
    items.addObject(done)

    doneToolbar.items = items
    doneToolbar.sizeToFit()

    doneToolbar.tintColor = UIColor(red: 240/225, green: 42/225, blue: 20/225, alpha: 1)
    doneToolbar.translucent = true

    self.toolbar = doneToolbar
    self.textView.inputAccessoryView = self.toolbar
}

func keyboardFrameDidChange(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
        var beginFrame = (userInfo[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.CGRectValue()

        let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
        let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
        let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)

        var animationDuration = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0

        var newFrame = self.textView.frame
        var keyboardFrameEnd = self.view.convertRect(endFrame!, toView: nil)
        var keyboardFrameBegin = self.view.convertRect(beginFrame!, toView: nil)

        newFrame.origin.y -= (keyboardFrameBegin.origin.y - keyboardFrameEnd.origin.y)
        self.textView.frame = newFrame

        UIView.animateWithDuration(animationDuration,
            delay: NSTimeInterval(0),
            options: animationCurve,
            animations: { self.view.layoutIfNeeded() },
            completion: nil)
    }

}

The above code only makes the textView move in a weird way and I don't know how to achieve the effect like the Message app. 上面的代码只会让textView以一种奇怪的方式移动,我不知道如何实现像Message应用程序那样的效果。

Update: 更新:

func keyboardFrameEnd(notification: NSNotification) {
    if let userInfo = notification.userInfo {
        var endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()

        // Change contentInset
        self.textView.contentInset.bottom = endFrame!.height
        self.textView.scrollIndicatorInsets.bottom = endFrame!.height
    }
}

I changed my code to above. 我将代码更改为上面的代码。 But sometimes when I release the keyboard halfway, the scrollView would suddenly bounce to the bottom which is very weird. 但有时当我中途释放键盘时,scrollView会突然弹到底部,这非常奇怪。 Any suggestion? 有什么建议吗?

UITextView is a scroll view. UITextView是一个滚动视图。 You do not need to resize it when the keyboard appears: you should adjust it's contentInset property, increasing the .bottom by the height of the keyboard. 当键盘出现时你不需要调整它的大小:你应该调整它的contentInset属性,将.bottom增加键盘的高度。

It can apply whether you have tabbar or not. 无论您是否有tabbar,它都可以适用。 And it would perfectly restore view that moved by keyboard to original position. 它将完美地恢复键盘移动到原始位置的视图。

// Set this up in storyboard first. 
// It's a vertical spacing constraint between your text view and bottom of superview.
@IBOutlet weak var bottomSpacingConstraint: NSLayoutConstraint! 

override func viewDidLoad() {
        super.viewDidLoad()            

        //    Receive(Get) Notification
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillShowNotification, object: nil)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardNotification:", name: UIKeyboardWillHideNotification, object: nil)


        self.originalConstraint = self.keyboardHeightLayoutConstraint?.constant //for original coordinate.
}

func keyboardNotification(notification: NSNotification) {
        let isShowing = notification.name == UIKeyboardWillShowNotification

        var tabbarHeight: CGFloat = 0
        if self.tabBarController? != nil {
            tabbarHeight = self.tabBarController!.tabBar.frame.height
        }
        if let userInfo = notification.userInfo {
            let endFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.CGRectValue()
            let duration:NSTimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as? NSNumber)?.doubleValue ?? 0
            let animationCurveRawNSN = userInfo[UIKeyboardAnimationCurveUserInfoKey] as? NSNumber
            let animationCurveRaw = animationCurveRawNSN?.unsignedLongValue ?? UIViewAnimationOptions.CurveEaseInOut.rawValue
            let animationCurve:UIViewAnimationOptions = UIViewAnimationOptions(rawValue: animationCurveRaw)
            self.keyboardHeightLayoutConstraint?.constant = isShowing ? (endFrame!.size.height - tabbarHeight) : self.originalConstraint!
            UIView.animateWithDuration(duration,
                delay: NSTimeInterval(0),
                options: animationCurve,
                animations: { self.view.layoutIfNeeded() },
                completion: nil)
        }
}

Move textfield when keyboard appears swift 当键盘快速显示时移动文本字段

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 键盘出现时调整UITextView的大小 - Resize the UITextView when keyboard appears 出现键盘时调整 UITextView 的大小 - Resize UITextView when keyboard appears 迅速出现键盘时滚动UITextView - Scroll UITextView when keyboard appears in swift 当用户单击UITextView并出现键盘时,如何使背景变暗/着色? - How to darken/tint background when user clicks UITextView and keyboard appears? 出现键盘时,如何正确设置UITextView的contentInset - How should I correctly set contentInset for UITextView when Keyboard appears iPad上的UIModalPresentationFormSheet。 键盘出现时如何调整UITextView高度 - UIModalPresentationFormSheet on iPad. How to adjust UITextView height when keyboard appears 当键盘显示UITextView而不是同一视图上的UITextField时,滚动视图 - Scroll view when keyboard appears for UITextView, but not for the UITextField that's on the same view 出现键盘时,带有大单元格的UITableViewController会变怪,隐藏UITextView - UITableViewController with large cells freaks out when a keyboard appears, hiding the UITextView UITextView在缩放以进行输出时隐藏文本 - UITextView hides text when scaled for output 创建UITextView时,如何在Live View中显示的Swift Playgrounds(在Xcode上)的UI中隐藏键盘? - How can I hide the keyboard in the UI in Swift Playgrounds (on Xcode) that appears in the Live View when I create a UITextView?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM