How do I make a view fit the width and half the height no matter the screen size?

sadelbridsadelbrid USMember
edited February 2017 in Xamarin.iOS

I'm trying to do something really basic. I want a view to fill half the height of the device and the entire width of the device. I want to use constraints to make this consistent across all screens. But I've had no luck. For the width, my idea was to set the leading and trailing edge constraint to 0. I'm not really sure the difference between Leading/Trailing and Left/Right constraints but I can't find Left/Right constraints even though I've seen people talk about them... Anyways I don't know how to manually add constraints. Well actually I know how to add Width, Height, and Aspect Ratio constraints by clicking the view and making go into "constraint" mode. I know how to add recommended constraints and edit them but it never gives me both Leading and Trailing. It's always one or the other. Can anybody help please? Thanks in advance.

Best Answer

Answers

  • yousufctecyousufctec USMember ✭✭✭
    edited February 2017

    Hi Seth,
    You have to use Grid for that one. Check the following.

      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="0.5" />
          <RowDefinition Height="0.5" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
    
        <Grid Grid.Row="0" Grid.Column="0">
          Your content goes here...
        </Grid>
      </Grid>
    

    Grid.Row="0" Grid.Column="0" is redundant as the default values will be 0.

    Thanks!

    Yousuf.

  • sadelbridsadelbrid USMember

    @yousufctec said:
    Hi Seth,
    You have to use Grid for that one. Check the following.

      <Grid>
        <Grid.RowDefinitions>
          <RowDefinition Height="0.5" />
          <RowDefinition Height="0.5" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        
        <Grid Grid.Row="0" Grid.Column="0">
          Your content goes here...
        </Grid>
      </Grid>
    

    Grid.Row="0" Grid.Column="0" is redundant as the default values will be 0.

    Thanks!

    Yousuf.

    Hey Yousuf. Thanks for the help! I'm new to iOS/Xamarin and I'm not sure where this markup goes... I know android has XML driving its UI but I don't see that for iOS storyboards. Where does this go? thanks!

  • StreammzStreammz NLMember

    Using a Xamarin.Forms project is not necessery if all you want is this feature.

    You can make something half the height of the parent by creating a normal height constraint, and then setting the constraint multiplier to 0.5

Sign In or Register to comment.