Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] On iOS, SafeArea returns Thickness of zero #2657

Closed
JohnHDev opened this issue Sep 23, 2021 · 25 comments · Fixed by #15512
Closed

[Bug] On iOS, SafeArea returns Thickness of zero #2657

JohnHDev opened this issue Sep 23, 2021 · 25 comments · Fixed by #15512
Assignees
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter delighter fixed-in-7.0.96 Look for this fix in 7.0.96 SR8! fixed-in-7.0.100 fixed-in-7.0.101 fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert partner Issue or Request from a partner team platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working

Comments

@JohnHDev
Copy link

JohnHDev commented Sep 23, 2021

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: There are no good versions of .NET MAUI
  • IDE:
  • Platform Target Frameworks:
    • iOS:
  • Affected Devices: iPhone 11 14.5 simulator

Workaround

None

@hartez hartez self-assigned this Sep 23, 2021
@Eilon Eilon added the area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter label Sep 23, 2021
@jsuarezruiz jsuarezruiz added t/bug Something isn't working platform/iOS 🍎 labels Oct 22, 2021
@mattscheffer
Copy link

mattscheffer commented Feb 16, 2022

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

image

@mattscheffer mattscheffer added the s/verified Verified / Reproducible Issue ready for Engineering Triage label Feb 16, 2022
@hartez hartez added this to the 6.0.300-servicing milestone Mar 22, 2022
Iga-Entap added a commit to entap/Entap.Basic.Maui that referenced this issue Jun 29, 2022
MAUIのバグにより、現状ではSafeAreaが取得できていない
dotnet/maui#2657
@hartez hartez removed their assignment Aug 2, 2022
@Redth Redth modified the milestones: 6.0-servicing, Backlog Aug 30, 2022
@ghost
Copy link

ghost commented Aug 30, 2022

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.

@KSemenenko
Copy link
Contributor

KSemenenko commented Nov 24, 2022

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
Copy link
Contributor

	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
Copy link
Contributor

KSemenenko commented Nov 24, 2022

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
Copy link
Contributor

KSemenenko commented Nov 24, 2022

C# version is working

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

@drmaven
Copy link

drmaven commented Nov 24, 2022

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.

@KSemenenko
Copy link
Contributor

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
Copy link
Contributor

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

@bcaceiro
Copy link

bcaceiro commented Dec 4, 2022

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

@Hooterr
Copy link

Hooterr commented Jan 11, 2023

Still not working to this day on .NET 7

@JohnHDev
Copy link
Author

@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.

@Hooterr
Copy link

Hooterr commented Jan 29, 2023

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...

@KSemenenko
Copy link
Contributor

@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
Copy link
Contributor

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

@JohnHDev
Copy link
Author

@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.

@KSemenenko
Copy link
Contributor

@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.

@kramer-e
Copy link

kramer-e commented Feb 21, 2023

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

@KSemenenko
Copy link
Contributor

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

@scriptBoris
Copy link

scriptBoris commented Mar 21, 2023

Disgusting work

@samhouts samhouts removed the s/verified Verified / Reproducible Issue ready for Engineering Triage label Apr 5, 2023
@jinxinjuan jinxinjuan added the s/triaged Issue has been reviewed label Apr 11, 2023
@QianaJiao
Copy link

QianaJiao commented Apr 11, 2023

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 QianaJiao added the s/verified Verified / Reproducible Issue ready for Engineering Triage label Apr 11, 2023
@Redth Redth added partner Issue or Request from a partner team delighter labels Apr 22, 2023
@tj-devel709 tj-devel709 self-assigned this Apr 24, 2023
@hartez
Copy link
Contributor

hartez commented May 4, 2023

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

@kramer-e
Copy link

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.

@KSemenenko
Copy link
Contributor

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

@Hooterr
Copy link

Hooterr commented May 19, 2023

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.

@samhouts samhouts modified the milestones: Backlog, .NET 8 Jul 12, 2023
@samhouts samhouts added the fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! label Aug 8, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Sep 7, 2023
@samhouts samhouts added the fixed-in-7.0.96 Look for this fix in 7.0.96 SR8! label Oct 10, 2023
@samhouts samhouts added the migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert label Sep 6, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-layout StackLayout, GridLayout, ContentView, AbsoluteLayout, FlexLayout, ContentPresenter delighter fixed-in-7.0.96 Look for this fix in 7.0.96 SR8! fixed-in-7.0.100 fixed-in-7.0.101 fixed-in-8.0.0-preview.7.8842 Look for this fix in 8.0.0-preview.7.8842! migration-compatibility Xamarin.Forms to .NET MAUI Migration, Upgrade Assistant, Try-Convert partner Issue or Request from a partner team platform/iOS 🍎 s/triaged Issue has been reviewed s/verified Verified / Reproducible Issue ready for Engineering Triage t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.