1

I am trying to create a simple stacklayout UI. Here is the XAML.

    <ContentView>
        <StackLayout
            BackgroundColor="Green"
            HeightRequest="500"
            VerticalOptions="End">
            <StackLayout
                BackgroundColor="LightSkyBlue"
                HeightRequest="100"
                VerticalOptions="End">
                <!--  // ADD CONTROLS HERE[!  -->
            </StackLayout>
        </StackLayout>
    </ContentView>

Ideally it is expected that the 'blue' stacklayout should align itself at the bottom on the green one But, it just doesn't moves and stays on the top.

What am i doing wrong? Please point me in the right direction

Attaching the image for better clarity.

enter image description here

Adam
  • 1,384
  • 1
  • 16
  • 27
Shailesh
  • 3,072
  • 3
  • 24
  • 33
  • Is the yellow the page background color? – Adam Feb 24 '20 at 19:19
  • Could you elaborate a bit more on what you are trying to achieve, maybe an example UI? The StackLayout in the StackLayout could be causing the issue. There may be a better way of building the UI that plays well with Forms. – Adam Feb 24 '20 at 19:31
  • @Adam- Just trying to align the blue stack view at the bottom of the green. – Shailesh Feb 24 '20 at 19:35
  • Yeah, I think that's clear enough from your question. I just think there may be a better approach to this depending on what you want to ultimately achieve. For example, if you wanted an image with a title at the bottom you could use a grid (https://github.com/kphillpotts/XamarinFormsLayoutChallenges/blob/master/GreatPlaces/GreatPlaces/GreatPlaces/MainPage.xaml). Maybe not though. – Adam Feb 24 '20 at 19:43
  • Is there anything that appears wrong ? why won't the stack views align themselves ? or is this is xamarin bug? – Shailesh Feb 24 '20 at 19:52
  • Nothing that appears wrong, just seemed like an odd use of controls, but what you are building may be very different than what I'm used to so that's why I was asking. – Adam Feb 24 '20 at 19:59
  • @Shailesh As Sümeyya Tuğçe Arar said that, I suggest you can set these two stacklayouts VerticalOptions as EndAndExpand, because EndAndExpand means that places the view at the end of the layout (bottom or right-most boundary) and expands to take up as much space as the layout will give it.You can take a look:[Xamarin.Forms StackLayout](https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/layouts/stack-layout) – Cherry Bu - MSFT Feb 25 '20 at 01:32

3 Answers3

4

You just need to replace your code by below code:

<StackLayout
        BackgroundColor="Green"
        HeightRequest="500"
        VerticalOptions="End">
        <StackLayout
            BackgroundColor="LightSkyBlue"
            HeightRequest="100"
            VerticalOptions="EndAndExpand">
            <!--  // ADD CONTROLS HERE[!  -->
        </StackLayout>
    </StackLayout>

Hope it will help you

Thank you

Rajendra Razz
  • 209
  • 1
  • 4
2

In my experience, a StackLayout will never obey Vertical or Horizontal options. It will only use as much space as it needs. You can place a transparent BoxView in the StackLayout and set its VerticalOptions to FillAndExpand. That will force the StackLayout to use all the space.

I normally use a Grid instead and use <RowDefinition Height="*"/> to force it to use all the space.

ottermatic
  • 992
  • 6
  • 10
1

You need to set blue Stack Layout's Vertical Option to EndAndExpand. Because stack layout children doesn't fill automatically. You need to force it if you want. You can look at this answer here for detailed explanation.

Tuğçe Arar
  • 776
  • 5
  • 20