The size and position (or insets) is not correct in iOS devices. What can I do?

Most of iOS devices are using point-based coordinate to decide position and size of view. However, Unity's Screen.height and Screen.width is using screen pixels instead of point. There is no way to decide correct insets without consideration of screen scale of the retina display of iOS devices. You could use UniWebViewHelper.screenHeight and UniWebViewHelper.screenWidth APIs to get the correct screen size of all devices (iOS or Android) and do you insets calculating based on those values. See the reference for more information.

Have more questions? Submit a request

7 Comments

  • 0
    Avatar
    Yumenosuke

    I have same problem.

    I try following delegate implementation.

    this code stretch size of UniWebView to a uGui element.

     

     

    public RectTransform rect; // attach an instance from editor

    UniWebViewEdgeInsets InsetsForScreenOreitation(UniWebView webView, UniWebViewOrientation orientation) {
    if (rect != null) {
    var corners = new Vector3 [4];
    rect.GetWorldCorners(corners);
    var l = corners.Min(c => c.x);
    var r = corners.Max(c => c.x);
    var b = corners.Min(c => c.y);
    var t = corners.Max(c => c.y);

    var canvas = rect.GetComponentInParent<Canvas>();
    var canvasRect = canvas.GetComponent<RectTransform>();
    var cvsCorners = new Vector3 [4];
    canvasRect.GetWorldCorners(cvsCorners);
    var cl = cvsCorners.Min(c => c.x);
    var cr = cvsCorners.Max(c => c.x);
    var cb = cvsCorners.Min(c => c.y);
    var ct = cvsCorners.Max(c => c.y);

    var it = ct - t;
    var il = l - cl;
    var ib = b - cb;
    var ir = cr - r;

    return new UniWebViewEdgeInsets((int)it, (int)il, (int)ib, (int)ir);
    }
    return webView.insets;
    }

     

    On UnityEditor or Android, this code works well.

    However, iOS not.

     

    Is this a bug of the asset or my mistake?

  • 1
    Avatar
    Bilel Mnasser

    i have same problem with setting inset top on ios devises !! 

    there some multiple of 2 values

  • 0
    Avatar
    Ad Adnan

    I have this issue as well !

  • 0
    Avatar
    Amir Levi

    anyone got it to work ?

  • 0
    Avatar
    Bilel Mnasser

    my app has a unity UI header (fixed 350 header height , you can change this to dynamic height) and Aspect Ratio of my app is 9:16 fixed :

    i edited the awake function of uniwebview.cs and added this at first :

     

     

    #if UNITY_ANDROID

    CanvasScaler scaler = canvas.GetComponent<CanvasScaler>();
    this.insets.top = Mathf.CeilToInt(350 * (Screen.height / scaler.referenceResolution.y)) - 1;

     

     


    #endif
    #if UNITY_IOS

    CanvasScaler scaler = canvas.GetComponent<CanvasScaler>();
    this.insets.top = Mathf.CeilToInt((350.0f * (Screen.height / scaler.referenceResolution.y))/2) - 1;


    #endif

    Edited by Bilel Mnasser
  • 0
    Avatar
    汪振

    I find the right way.


        public static void UniViewInsetsForIOS(UniWebView webView, UniWebViewOrientation orientation, RectTransform rect, Camera camera)
        {
            Vector3[] corners = new Vector3[4];
            rect.GetWorldCorners(corners);

            float ratio = 1;
            if (orientation == UniWebViewOrientation.Portrait)
                ratio = UniWebViewHelper.screenScale;
            else if (orientation == UniWebViewOrientation.LandScape)
                ratio = 1f / UniWebViewHelper.screenScale;
            
            float c1y = camera.WorldToScreenPoint(corners[1]).y * ratio;
            int top = UniWebViewHelper.screenHeight - (int)c1y;

            float c3y = camera.WorldToScreenPoint(corners[3]).y * ratio;
            int bottom = (int)c3y;

            float c1x = camera.WorldToScreenPoint(corners[1]).x * ratio;
            int left = (int)c1x;

            float c3x = camera.WorldToScreenPoint(corners[3]).x * ratio;
            int right = UniWebViewHelper.screenWidth - (int)c3x;

            webView.insets = new UniWebViewEdgeInsets(top, left, bottom, right);
        }

    Edited by 汪振
  • 0
    Avatar
    Amir Levi

    I have used Yumenosuke method and fixed it to fit my needs.

    My render mode in canvas and my width and height there is: 10.24,7.68.

     public static UniWebViewEdgeInsets InsetsForScreenOreitation( UniWebViewOrientation orientation, RectTransform rect) 
            {
                var corners = new Vector3 [4];
                rect.GetWorldCorners(corners);

                var l = corners.Min(c => c.x);
                var r = corners.Max(c => c.x);
                var b = corners.Min(c => c.y);
                var t = corners.Max(c => c.y);

                var canvas = rect.GetComponentInParent<Canvas>();
                var canvasRect = canvas.GetComponent<RectTransform>();
                var cvsCorners = new Vector3 [4];
                canvasRect.GetWorldCorners(cvsCorners);
                var cl = cvsCorners.Min(c => c.x);
                var cr = cvsCorners.Max(c => c.x);
                var cb = cvsCorners.Min(c => c.y);
                var ct = cvsCorners.Max(c => c.y);

                float _ratioX = UniWebViewHelper.screenWidth / (cr - cl);
                float _ratioY = UniWebViewHelper.screenHeight / (ct - cb);

                var it = (ct - t) * _ratioY ;
                var il = (l - cl) * _ratioX;
                var ib = (b - cb)  * _ratioY;
                var ir = (cr - r) * _ratioX;

                return new UniWebViewEdgeInsets((int)it, (int)il, (int)ib, (int)ir);
            }

     

    Hope that will help.

Please sign in to leave a comment.
Powered by Zendesk