jho
jho

Reputation: 293

How to get Editor in Xamarin Forms iOS to auto resize and be scrollable with its content?

I have a ScrollView with an Editor content inside.

I'm creating a custom Editor which resizes dependant on text inside it.

        <ScrollView x:Name="sv" Grid.Row="2" HorizontalOptions="Fill" VerticalOptions="Fill" Orientation="Both"
                        HorizontalScrollBarVisibility="Always" VerticalScrollBarVisibility="Always" BackgroundColor="LightGreen">
            <local:EditorEx x:Name="ed" Margin="0" BackgroundColor="LightBlue" HorizontalOptions="Start" VerticalOptions="Start"/>
        </ScrollView>

I've successfully managed to get the editor to dynamically resize the editor based on this code placed in the renderer:

How do I size a UITextView to its content?

      void Init()
        {
            //needed for below code to work
        Control.ScrollEnabled = false;
        }

        private void Control_Changed(object sender, System.EventArgs e)
        {
            CGSize newSize = Control.SizeThatFits(new CGSize(nfloat.MaxValue, nfloat.MaxValue));
            CGRect newFrame = Control.Frame;
            newFrame.Size = new CGSize(newSize.Width, newSize.Height);
            //Control.Frame = newFrame;
  
            //this is the xamarin Editor control
            editor.WidthRequest = newSize.Width;
            editor.HeightRequest = newSize.Height;
        }

However, the ScrollView does not dynamically re-adjust according to the new editor size and hence I can't scroll to the newly added text.

How can I resolve this?

Upvotes: 0

Views: 443

Answers (1)

ToolmakerSteve
ToolmakerSteve

Reputation: 21213

To get a layout to recalculate its display, after a programmatic change that affects it, try ForceLayout:

sv.ForceLayout();

In most cases this happens automatically, but ScrollView does not realize that a change to its contents may require this.

Upvotes: 1

Related Questions