diff --git a/Chat/Chat/ChatInput/ChatInputView.cs b/Chat/Chat/ChatInput/ChatInputView.cs index ff404035..329be888 100644 --- a/Chat/Chat/ChatInput/ChatInputView.cs +++ b/Chat/Chat/ChatInput/ChatInputView.cs @@ -16,7 +16,7 @@ namespace Chat const float BorderWidth = 0.5f; const float CornerRadius = 5; - const float ToolbarMinHeight = 44; + public static readonly float ToolbarMinHeight = 44; public UITextView TextView { get; private set; } public UIButton SendButton { get; private set; } diff --git a/Chat/Chat/ChatViewController.cs b/Chat/Chat/ChatViewController.cs index b90a6582..923d43fd 100644 --- a/Chat/Chat/ChatViewController.cs +++ b/Chat/Chat/ChatViewController.cs @@ -97,7 +97,16 @@ namespace Chat void AddObservers() { - TextView.AddObserver ("contentSize", NSKeyValueObservingOptions.OldNew, OnSizeChanged); + TextView.AddObserver(this,"contentSize", NSKeyValueObservingOptions.OldNew, IntPtr.Zero); + } + + public override void ObserveValue (NSString keyPath, NSObject ofObject, NSDictionary change, IntPtr context) + { + if (keyPath == "contentSize") { + OnSizeChanged (new NSObservedChange (change)); + } else { + base.ObserveValue (keyPath, ofObject, change, context); + } } void OnSizeChanged(NSObservedChange change) @@ -106,13 +115,39 @@ namespace Chat CGSize newValue = ((NSValue)change.NewValue).CGSizeValue; var dy = newValue.Height - oldValue.Height; + AdjustInputToolbarOnTextViewSizeChanged (dy); + } + + void AdjustInputToolbarOnTextViewSizeChanged(nfloat dy) + { + bool isIncreasing = dy > 0; + if (IsInputToolbarHasReachedMaximumHeight () && isIncreasing) { + // TODO: scroll to bottom + return; + } + + nfloat oldY = Chat.Frame.GetMinY (); + nfloat newY = oldY - dy; + if (newY < TopLayoutGuide.Length) + dy = oldY - TopLayoutGuide.Length; + AdjustInputToolbar (dy); } + bool IsInputToolbarHasReachedMaximumHeight() + { + return Chat.Frame.GetMinY () == TopLayoutGuide.Length; + } + void AdjustInputToolbar(nfloat change) { ToolbarHeightConstraint.Constant += change; + if (ToolbarHeightConstraint.Constant < ChatInputView.ToolbarMinHeight) + ToolbarHeightConstraint.Constant = ChatInputView.ToolbarMinHeight; + + Console.WriteLine (ToolbarHeightConstraint.Constant); + View.SetNeedsUpdateConstraints (); View.LayoutIfNeeded (); } @@ -146,10 +181,10 @@ namespace Chat void UpdateButtomLayoutConstraint(UIKeyboardEventArgs e) { - BottomConstraint.Constant = View.Bounds.GetMaxY () - e.FrameEnd.GetMinY (); - UIViewAnimationCurve curve = e.AnimationCurve; UIView.Animate (e.AnimationDuration, 0, ConvertToAnimationOptions (e.AnimationCurve), () => { + BottomConstraint.Constant = View.Bounds.GetMaxY () - e.FrameEnd.GetMinY (); + View.SetNeedsUpdateConstraints(); View.LayoutIfNeeded (); // Move content with keyboard @@ -233,7 +268,6 @@ namespace Chat void OnTextChanged (object sender, EventArgs e) { UpdateButtonState (); - } void UpdateButtonState()