Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ensure a StackLayout is at least a minimum height?

I tried this in my code.

<StackLayout 
   HorizontalOptions="FillAndExpand" 
   Spacing="0" 
   MinimumHeightRequest="50">
   <StackLayout Margin="20, 6, 20, 8" 
      HorizontalOptions="FillAndExpand" 
      VerticalOptions="FillAndExpand" 
      Spacing="0">
      <Label Text="ABC" 
         HorizontalOptions="Start" 
         VerticalOptions="StartAndExpand" />
   </StackLayout>
</StackLayout>

But with small text such as ABC then the height is not equal to 50. Is there some way I can ensure the height is a minimum of 50?

like image 494
Alan2 Avatar asked Aug 26 '18 11:08

Alan2


3 Answers

You can try to set HeightRequest=50 and measure height of your label text. Use the ITextMeter from this source, and if text height is more than 50, then set HeightRequest to text height.

like image 169
Денис Чорный Avatar answered Oct 09 '22 09:10

Денис Чорный


You would try something like that:

<Grid RowSpacing="0">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <StackLayout Grid.Row="0" HeightRequest="50"/>
</Grid>

I'm doing that in my application, and it works fine. You set the RowDefinition to Auto, then, it is going to fit exactly your HeightRequest.

like image 5
Daniel Cunha Avatar answered Oct 16 '22 15:10

Daniel Cunha


Relaying what ShawnCastrianni.5092 on the following forum

After reading the remarks in the API docs, I see this:

"MinimumHeightRequest is used to override the results of a call to VisualElement.GetSizeRequest (double, double) by setting the minimum height property. This causes overflow handling to shrink this element to its minimum height before elements who do not have a minimum size set."

I guess that matches what you said Jason. Therefore, I think everybody will be thrown off by the name MinimumHeightRequest. I would prefer it say OverflowHeightRequest to avoid any confusion.

The name itself is misleading. You'll have to create a custom solution to fix your issue

like image 4
LeRoy Avatar answered Oct 16 '22 15:10

LeRoy