UIView atop the Keyboard similar to iMessage App

前端 未结 5 1173
眼角桃花
眼角桃花 2020-12-04 14:38

currently I\'m attempting to basically implement and exact copy of Apples iMessage App.

That means I need a UITextView that is docked at the bottom of the screen, an

5条回答
  •  孤城傲影
    2020-12-04 15:15

    I have been successful at solving the problem in quite an elegant manner (I think,...).

    The code will be released on Github next week and linked to in this answer.

    --

    How it's done: I made the rotation work by choosing the inputAccessoryView-way of doing it.

    Nomenclature:

    1. 'MessageInputView' is a UIView containing my 'GrowingUITextView' (it also contains a "Send" Button and the background image).
    2. 'ChatView' is the view that belongs to the ChatViewController that displays all the Chatbubbles and has my 'MessageInputView' docked at the bottom.
    3. 'keyboardAccessoryView' is an empty UIView sized: CGRect(0,0,0,0).

    I needed to figure out how to have the MessageInputView stick around on the screen when the keyboard was dismissed. That was the tricky part. I did this by creating another view (keyboardAccessoryView) and had my GrowingUITextView use it as its inputAccessoryView. I retained the keyboardAccessoryView because I'd need the reference to it later on.

    Then I remembered some of the stuff I did in my other attempt (animating the MessageInputView's frames around the screen whenever a keyboard notification arrived).

    I added my MessageInputView as a subview to my ChatView (at the very bottom). Whenever it is activated and the willShow: methods is called by a keyboard notification, I manually animate the MessageInputView's frame to it's designated position up top. When the animation finishes and the completion block executes I remove the subview from the ChatView and add it to the keyboardAccessoryView. This causes another notification to be fired off because the keyboard is re-loaded EVERY time the inputAccessoryView's frame/bounds are changed!. You need to be aware of that and handle it appropriately!

    When the keyboard is about to dismissed, I convert my MessageInputView's frame to my ChatView's coordinate system and add it as a subview. Thus it is removed from my keyboardAccessoryView. I then resize the keyboardAccessoryView's frame back to CGRect(0,0,0,0) because otherwise the UIViewAnimationDuration will not match! Then I allow the keyboard to be dismissed and I have my MessageInputView follow it from above and eventually dock at the bottom of the screen.

    This is quite a lot of work for very little gain though.

    --

    Take care.

    PS: If someone figures out an easier way to do it (perfectly) let me know.

提交回复
热议问题