Editor not scrolling correctly with Keyboard Up and VerticalTextAlignment
- Dominant language
- C#
- Stars
- 23.3k
- Forks
- 2k
- Avg merge
- 1d 14h
- Merged PRs (30d)
- 296
Description
### Description
On iOS, when you are typing inside an editor and the following are true
* the soft keyboard is up
* VerticalTextAlignment is Center or End (and maybe Start)
* You have not filled in the editor with content yet
You are not able to scroll the content all the way up or down.
Inside MauiTextView, we have the following:
```cs
void ShouldCenterVertically()
{
var contentHeight = ContentSize.Height;
var availableSpace = Bounds.Height - contentHeight * ZoomScale;
if (availableSpace <= 0)
return;
ContentOffset = VerticalTextAlignment switch
{
Maui.TextAlignment.Center => new CGPoint(0, -Math.Max(1, availableSpace / 2)),
Maui.TextAlignment.End => new CGPoint(0, -Math.Max(1, availableSpace)),
_ => ContentOffset,
};
// Scroll the content to the cursor position if it is hidden by the keyboard
if (KeyboardAutoManagerScroll.IsKeyboardShowing && (VerticalTextAlignment == Maui.TextAlignment.Center || VerticalTextAlignment == Maui.TextAlignment.End))
{
var cursorRect = KeyboardAutoManagerScroll.FindCursorPosition();
var keyboardTop = KeyboardAutoManagerScroll.KeyboardFrame.Top;
if (cursorRect.HasValue && cursorRect.Value.Bottom > keyboardTop)
{
var offset = cursorRect.Value.Bottom - KeyboardAutoManagerScroll.KeyboardFrame.Top;
ContentOffset = new CGPoint(ContentOffset.X, ContentOffset.Y + offset);
}
}
}
```
When the availableSpace is 0 or less, we return early and no longer have these issues. This looks like the ContentOffset we apply is the issue. There are also ContentInsets being applied inside the `KeyboardAutoManagerScroll.ApplyContentInset(UIScrollView? scrolledView, UIScrollView? lastScrollView, bool didMove, bool isInnerEditor)` method which is something to look out for when fixing this issue.
This issue is brought up in this issue: https://github.com/dotnet/maui/pull/25827. This issue existed before this PR by the way.
Here are two examples of the issue
https://github.com/user-attachments/assets/b55a4e93-68c0-4e09-9820-b8020fd86d61
### Steps to Reproduce
1. Open the Controls.TestCases.HostApp on iOS device
2. Navigate to Issue 24977 page.
3. Select a VerticalTextAlignment option and then enter significant enters or text but do not fill up the entire editor
4. While the keyboard is still open, try to scroll the content back up and you will not be able to do that.
### Link to public reproduction project repository
_No response_
### Version with bug
9.0.50 SR5
### Is this a regression from previous behavior?
No, this is something new
### Last version that worked well
Unknown/Other
### Affected platforms
iOS
### Affected platform versions
_No response_
### Did you find any workaround?
_No response_
### Relevant log output
```shell
```
Contributor guide
Research direction
Start in MauiTextView at ShouldCenterVertically and inspect how ContentOffset is applied when VerticalTextAlignment is Center or End and the keyboard is visible. Then trace KeyboardAutoManagerScroll.ApplyContentInset(UIScrollView? scrolledView, UIScrollView? lastScrollView, bool didMove, bool isInnerEditor). Reproduce on iOS through Controls.TestCases.HostApp, using the Issue 24977 page; done means the editor can scroll fully up and down with the keyboard open.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, ios
- Domain
- mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100