Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking whether the user is using an iPhone or and iPad in Xamarin.Forms

I have been looking around for a while, and I cannot figure out how to check whether the user has an iPad or an iPhone?

I am using Xamarin.Forms, and I have no need to check whether the Android device is a tablet or phone as the issue does not resolve around Android.


Code which I have found that could be helpful:

var idiom = UIKit.UIDevice.CurrentDevice.UserInterfaceIdiom;

Thanks for your time!

like image 243
Daniel Avatar asked Jan 06 '18 18:01

Daniel


1 Answers

You can check this by using the Device Class in Xamarin.Forms

if (Device.Idiom == TargetIdiom.Tablet)
{
    // iPad
}

if (Device.Idiom == TargetIdiom.Phone)
{
    // iPhone
}

This is a multi-platform solution that works for Android, iOS and Windows Phone.

If you need to make differences in XAML files you can use the OnIdiom tag like this:

<StackLayout>
   <StackLayout.Padding>
      <OnIdiom x:TypeArguments="Thickness">
          <OnIdiom.Phone>
              10
          </OnIdiom.Phone>
          <OnIdiom.Tablet>
              20
          </OnIdiom.Tablet>
      </OnIdiom>
   </StackLayout.Padding>
</StackLayout>
like image 153
Trevi Awater Avatar answered Sep 28 '22 00:09

Trevi Awater