当我移动它时,UIView容器消失了

时间:2016-01-03 12:56:23

标签: ios swift uiview

我有UIView,它是按钮的容器"发送"和textView。 当我开始编辑时,我在屏幕上有键盘,我更改容器的Y原点以将其移动到更高的位置而不被键盘覆盖。

当我完成编辑时,我隐藏了键盘,我的容器(带按钮和textView的UIView)就消失了!但如果我向右或向左转动模拟器,这又会变得可见。

以下是上下移动容器的动画代码:

func textViewDidChange(textView: UITextView) {

    self.messagePromptLabel.hidden = messageTextView.hasText() ? true : false

}

func textViewDidEndEditing(textView: UITextView) {
    if !self.messageTextView.hasText()  {
        self.messagePromptLabel.hidden = false
    }
}

func didTapScrollView(){
    self.view.endEditing(true)
}

func keyboardWasShown(notification: NSNotification) {

    let dict: NSDictionary = notification.userInfo!
    let keyboardSize: NSValue = dict.valueForKey(UIKeyboardFrameEndUserInfoKey) as! NSValue
    let frameKeyboardSize: CGRect = keyboardSize.CGRectValue()

    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.chatScrollView.frame.origin.y -= frameKeyboardSize.height
        self.messageView.frame.origin.y -= frameKeyboardSize.height

        }) { (finished: Bool) -> Void in

    }

}

func keyboardWillHide(notification: NSNotification) {

    UIView.animateWithDuration(0.3, animations: { () -> Void in
        self.chatScrollView.frame.origin.y = self.chatScrollViewOriginY!
        self.messageView.frame.origin.y = self.messageTextViewOriginY!

        }) { (finished: Bool) -> Void in
    }
}

一些额外的代码 这段代码可能有助于找到一些东西。

@IBOutlet weak var messageTextView: UITextView!
@IBOutlet weak var chatScrollView: UIScrollView!
@IBOutlet weak var messagePromptLabel: UILabel!
@IBOutlet weak var messageView: UIView!

var chatScrollViewOriginY: CGFloat?
var messageTextViewOriginY: CGFloat?

var messageArray = [String]()
var senderArray = [String]()

var currentUserImage: UIImage?
var recipientImage: UIImage?

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
    messageTextView.addSubview(messagePromptLabel)
    self.title = recipientNickname

    chatScrollViewOriginY = self.chatScrollView.frame.origin.y
    messageTextViewOriginY = self.messageTextView.frame.origin.y
    print(" messageTextViewOriginY \(messageTextViewOriginY)")

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWasShown:", name: UIKeyboardDidShowNotification, object: nil)

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)

    let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: "didTapScrollView")
    tapGestureRecognizer.numberOfTouchesRequired = 1
    chatScrollView.addGestureRecognizer(tapGestureRecognizer)
    chatScrollView.backgroundColor = UIColor.redColor()
}

func textViewDidChange(textView: UITextView) {

    self.messagePromptLabel.hidden = messageTextView.hasText() ? true : false

}

func textViewDidEndEditing(textView: UITextView) {
    if !self.messageTextView.hasText()  {
        self.messagePromptLabel.hidden = false
    }
}

func didTapScrollView(){
    self.view.endEditing(true)
}

问题: 是什么让我的UIView容器在我隐藏键盘并且当我向左或向右转动手机时可见时始终不可见?

1 个答案:

答案 0 :(得分:1)

您需要在发送按钮操作中添加此代码:

self.view.endEditing(true)