maui icon indicating copy to clipboard operation
maui copied to clipboard

[Bug] On iOS, SafeArea returns Thickness of zero

Open JohnHDev opened this issue 4 years ago • 9 comments

Given: var safeArea = On<Microsoft.Maui.Controls.PlatformConfiguration.iOS>().SafeAreaInsets();

SafeAreaInserts is returning a Thickness of zero on iPhone 11 simulator running iOS 14.5.

Steps to Reproduce

  1. Add the above code to a Maui project
  2. Set a breakpoint
  3. Run it

Expected Behavior

Should return correct safe area

Actual Behavior

Returns an empty safe area

Basic Information

  • Version with issue: Maui Preview 8
  • Last known good version:
  • IDE:
  • Platform Target Frameworks:
    • iOS:
  • Affected Devices: iPhone 11 14.5 simulator

Workaround

None

JohnHDev avatar Sep 23 '21 12:09 JohnHDev

Bug repros in 17.2 Preview 1. Tested with iPhone 13 iOS 15.2. Repro project is here: 2657.zip

image

mattscheffer avatar Feb 16 '22 01:02 mattscheffer

We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process.

ghost avatar Aug 30 '22 15:08 ghost

I have the same isuse, iPhone 14 Max iOS 16 Looks like SafeArea is applied, but

On<iOS>().SetUseSafeArea(false);

has no effect, also

On<iOS>().SetSafeAreaInsets(new Thickness(100, 100, 100, 0));

has no effect.

.NET 7

KSemenenko avatar Nov 24 '22 12:11 KSemenenko

	internal static UIEdgeInsets SafeAreaInsetsForWindow
		{
			get
			{
				UIEdgeInsets safeAreaInsets;

				if (!Forms.IsiOS11OrNewer)
					safeAreaInsets = new UIEdgeInsets(UIApplication.SharedApplication.StatusBarFrame.Size.Height, 0, 0, 0);
				else if (UIApplication.SharedApplication.GetKeyWindow() != null)
					safeAreaInsets = UIApplication.SharedApplication.GetKeyWindow().SafeAreaInsets;
#pragma warning disable CA1416, CA1422  // TODO: UIApplication.Windows is unsupported on: 'ios' 15.0 and later
				else if (UIApplication.SharedApplication.Windows.Length > 0)
					safeAreaInsets = UIApplication.SharedApplication.Windows[0].SafeAreaInsets;
#pragma warning restore CA1416, CA1422
				else
					safeAreaInsets = UIEdgeInsets.Zero;

				return safeAreaInsets;
			}
		}

for me UIApplication.SharedApplication.Windows.Length is 0

KSemenenko avatar Nov 24 '22 12:11 KSemenenko

looks like "windows" is now deprecated too.

looks like for iOS 15 it shhould be like this

extension UIApplication {
    static var yourNameByValue: UIEdgeInsets  {
        let scene = UIApplication.shared.connectedScenes.first as? UIWindowScene
        return scene?.windows.first?.safeAreaInsets ?? .zero
    }
}

KSemenenko avatar Nov 24 '22 12:11 KSemenenko

C# version is working

var scene = UIKit.UIApplication.SharedApplication.ConnectedScenes.ToArray().FirstOrDefault();
		var windowScene = (UIKit.UIWindowScene)scene;
		var safeArea = windowScene.Windows.FirstOrDefault()?.SafeAreaInsets;

KSemenenko avatar Nov 24 '22 13:11 KSemenenko

Adding to the previous comments, there is something wrong with SafeArea on iPhones, since if SafeArea is set to false and the phone is horizontal, the app crashes.

drmaven avatar Nov 24 '22 14:11 drmaven

and in Maui

static UIWindow? GetKeyWindow()
{
	// if we have scene support, use that
	if (OperatingSystem.IsIOSVersionAtLeast(13) || OperatingSystem.IsMacCatalystVersionAtLeast(13))
	{
		try
		{
			using var scenes = UIApplication.SharedApplication.ConnectedScenes;
			var windowScene = scenes.ToArray<UIWindowScene>().FirstOrDefault();
			return windowScene?.Windows.FirstOrDefault();
		}
		catch (InvalidCastException)
		{
			// HACK: Workaround for https://github.com/xamarin/xamarin-macios/issues/13704
			//       This only throws if the collection is empty.
			return null;
		}
	}

	// use the windows property (up to 13.0)
	return UIApplication.SharedApplication.KeyWindow;
}

but for me sometime this windowScene?.Windows still is empty

KSemenenko avatar Nov 24 '22 14:11 KSemenenko

but anyway, if I use this code (copied from MAUI) it works. but On<iOS>().SafeAreaInsets() is not Screenshot 2022-11-24 at 15 39 32

KSemenenko avatar Nov 24 '22 14:11 KSemenenko

Having the same issue, setting SafeArea to false, has no response, specially in landscape mode, which is a blocker to me

bcaceiro avatar Dec 04 '22 08:12 bcaceiro

Still not working to this day on .NET 7

Hooterr avatar Jan 11 '23 09:01 Hooterr

@Redth @hartez Any idea when this will get picked up? Ive just tried it again in our .NET MAUI test app with the latest Maui workload, still broken. This bug was reported 16 months ago, during Maui preview, was verified, others are reporting the same issue and yet it still isn't fixed.

Why should we report bugs when they don't get fixed? You are wasting my time, Im not paid to debug .NET MAUI for you, I had enough of doing that for Xamarin Forms.

I can't sell migrating to .NET MAUI from Xamarin Forms when simple bugs like this don't get picked up after reporting them over a year ago. Our Xamarin Forms code base is full of workarounds for various issues that never got resolved, we don't want to have to do the same for .NET MAUI and we won't start migrating until Maui is much more stable.

JohnHDev avatar Jan 29 '23 09:01 JohnHDev

Looking at the sprint board, there are 37 items planned for the current sprint. There are 2,2k issues reported, to fix them all would take 2,5 yrs at this pace (assuming no new bugs are reported during that time). I think it's safe to assume that we're on our own...

Hooterr avatar Jan 29 '23 11:01 Hooterr

@JohnHDev any way you have to migtate your code to MAUI in some day, because xamarin will be depricated. so I just copy-past my old code from XF into Maui, all of us know how to fix issues by yourself, so this is not a problem =) kind of annoyng but not a problem

KSemenenko avatar Jan 29 '23 11:01 KSemenenko

I fill this issue relatod to pages, or time of createing page or so

KSemenenko avatar Jan 29 '23 11:01 KSemenenko

@JohnHDev any way you have to migtate your code to MAUI in some day, because xamarin will be depricated. so I just copy-past my old code from XF into Maui, all of us know how to fix issues by yourself, so this is not a problem =) kind of annoyng but not a problem

@KSemenenko We don't have to migrate to .NET MAUI at all, there are alternatives and we might have the budget to move to a different framework. I'd rather not, I'd rather see better support than... this.

JohnHDev avatar Jan 29 '23 13:01 JohnHDev

@JohnHDev I have now switched to MAUI and Blazor Mobile, and I really like C#, but at this point I'm not sure it's worth it either =( I don't know what to say about the native binding of ios and android, the broken tooling and the inability to debug applications on devices that make me struggle with something that is supposed to work, not my job.

KSemenenko avatar Jan 29 '23 13:01 KSemenenko

This is basic functionality, why is this issue still open after such a long time? I agree, we shouldn't be wasting time on these issues after MAUI GA.

As KSemenenko said, calling the same code directly works, so it should be an easy fix:

var safeAreaInsets = Microsoft.Maui.ApplicationModel.WindowStateManager.Default.GetCurrentUIWindow().SafeAreaInsets;

Probably related to #12823 and #5856

kramer-e avatar Feb 21 '23 22:02 kramer-e

@kramer-e i think our best case is .net8

KSemenenko avatar Feb 21 '23 22:02 KSemenenko

Disgusting work

scriptBoris avatar Mar 21 '23 21:03 scriptBoris

I installed the 2657.zip and update the sdk version to .NET 7. Then verified this issue still repro with Visual Studio Enterprise 17.6.0 Preview 2.0. image

QianaJiao avatar Apr 11 '23 08:04 QianaJiao

Out of curiosity, what are folks using the SafeAreaInsets property for?

hartez avatar May 04 '23 15:05 hartez

I use this for a workaround on iOS, because of the issue that View.IgnoreSafeArea doesn't work on .NET 7 #12823

I need to show content behind the statusbar and controls below that in the safe area. I've set ios:Page.UseSafeArea="False" on the page to ignore the SafeArea (content behind status bar) and use the SafeAreaInsets to set the margin on controls such as an Entry and Buttons.

kramer-e avatar May 17 '23 22:05 kramer-e

Out of curiosity, what are folks using the SafeAreaInsets property for?

To make design good locking on iOS, especially bottom, where you no need bold line with background color.

like map, you want full screen map, or the same ful screen map but with margins only on top

KSemenenko avatar May 19 '23 05:05 KSemenenko

It's very often that you need some layouts, or images to go beyond the safe area. I know that as a workaround you can set BackgroundColor of a page, which will in turn change the color of those elements, but you can't control the color of bottom and top safe area separately. To sum up, when you build anything else than hello world you will need this to work.

Hooterr avatar May 19 '23 09:05 Hooterr