简体   繁体   中英

Is there something similar to Android's dimens.xml in iOS?

I recently found out that I should be placing all my sizing/dimensions in dimens.xml for Android applications that I want to support multiple screen resolutions.

Is there something similar that I should do in iOS to support multiple screen resolutions? I code all my views in the back end (no storyboards) and when I want to position a view (padding, margins, etc.) I simply add hard-coded numbers to the X and Y properties of the view's frame . Same thing for Width , Height , Font size , etc. I imagine my iOS apps that I coded like this will look awkward on different screen resolutions like this?

By the way, I'm using Xamarin (Xamarin.Android and Xamarin.iOS). But I would understand answers using native code.

There is nothing like dimens in iOS.

I believe proper constraints with AutoLayout can work well on padding , margins , Width , Height ,etc.

However, if you still want to find a way to achieve it , here is a temporary workaround.

Create a static Class named Dimens .

public static class Dimens
{
    public static int Height
    {
        get {
            string device = DeviceHardware.Model;

            if (device.Contains("iPhone4"))
            {
                return 10;
            }
            else if (device.Contains("iPhone5"))
            {
                return 15;
            }
            else if (device.Equals("iPhone 6") || device.Equals("iPhone 6S"))
            {
                return 20;
            }
            else if (device.Equals("iPhone 6 Plus") || device.Equals("iPhone 6S Plus"))
            {
                return 25;
            }
            //other device 

            return 0;
        }
    }      
}

Usage:

View.Height = Dimens.Height;

Here is the plugin which can check your device type.

you can create an empty swift file, and use struct instead of dimens.xml for Android

struct Padding {
    static let left = 10
    static let right = 10
    static let top = 10
    static let bottom = 10
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM