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] Error using popup on iOS #1844

Closed
2 tasks done
acaliaro opened this issue Apr 29, 2024 · 15 comments · Fixed by #1892
Closed
2 tasks done

[BUG] Error using popup on iOS #1844

acaliaro opened this issue Apr 29, 2024 · 15 comments · Fixed by #1892
Labels
area/views Issue/Discussion/PR that has to do with Views bug Something isn't working unverified

Comments

@acaliaro
Copy link

acaliaro commented Apr 29, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Did you read the "Reporting a bug" section on Contributing file?

Current Behavior

I show a popup. When I close the popup, I have an exception

 An error occurred: 'Object reference not set to an instance of an object.'. Callstack: '   at CommunityToolkit.Maui.Core.Views.MauiPopup.SetShadowView(UIView& target)
   at CommunityToolkit.Maui.Core.Views.MauiPopup.ViewDidLayoutSubviews()
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at daze.dazeapp.Program.Main(String[] args) in C:\Daze\daze.app.maui\daze.dazeapp\daze.dazeapp\Platforms\iOS\Program.cs:line 12
   at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Method(Object obj, IntPtr* args)
   at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)'

I think the problem is that "target" sometimes is null

static void SetShadowView(in UIView target)
{
	if (target.Class.Name is "_UICutoutShadowView")
	{
		target.RemoveFromSuperview();
	}

	if (target.Class.Name is "_UIPopoverDimmingView")
	{
		target.BackgroundColor = UIColor.Black.ColorWithAlpha(0.4f);
	}

	foreach (var view in target.Subviews)
	{
		SetShadowView(view);
	}
}

maybe a check on "target" and "target.Subviews" should solve the problem...

Expected Behavior

It should not crash

Steps To Reproduce

I have a popup that I Use as loader... when I close the popup after some "works", sometimes the exception is thrown

Link to public reproduction project repository

I haven't one...

Environment

- .NET MAUI CommunityToolkit:8.0.1
- OS:iOS 17.3
- .NET MAUI: 8.0.21

Anything else?

No response

@acaliaro acaliaro added bug Something isn't working unverified labels Apr 29, 2024
Copy link
Contributor

Hi @acaliaro. We have added the "needs reproduction" label to this issue, which indicates that we cannot take further action. This issue will be closed automatically in 5 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@acaliaro
Copy link
Author

@brminnick sorry but I am not able to create a repo. The problem appears in a very large app.

The information I sent you on the crash seems quite clear and the null check of the object is also punctual. Do you think the change can be made?
Thanks!

@brminnick
Copy link
Collaborator

I can't fix the bug if I cannot identify the root cause. I can't identify the root cause without a reproduction.

@bijington
Copy link
Contributor

@acaliaro how about trying to make that change locally and see if that solves your problem? Then if we can gain confidence it solves the issue your changes could make it into a PR

@SumitMandal
Copy link

SumitMandal commented Apr 30, 2024

@brminnick I am also facing this issue on the iPhone 11 or lower. The iOS version does not matter.

Steps to reproduce(1st approach):

  1. Open CommunityToolkit.Maui.Sample.sln in the samples folder.
  2. Choose the target device as iPhone 11 or lower
  3. Run the project
  4. Open Flyout window
  5. Choose "Views"
  6. Choose "Multi Popups Page" card
  7. Click "Simple Popup" button
  8. Once the Popup opens rotate the device, when you rotate the device for the third time the app will crash.

Steps to reproduce(2nd approach):

  1. Open CommunityToolkit.Maui.Sample.sln in the samples folder.
  2. Choose the target device as iPhone 11 or lower
  3. Run the project
  4. Rotate the device one time
  5. Open Flyout window
  6. Choose "Views"
  7. Choose "Multi Popups Page" card
  8. Click "Simple Popup" button
  9. The app will crash

The reason behind the crash is the PresentationController.ContainerView that is being passed in the SetShadowView method is null.

SetShadowView(PresentationController.ContainerView);

1st approach Demo:
https://github.com/CommunityToolkit/Maui/assets/5452331/2da060f3-143d-4ecc-8bfe-0961efce5509

2nd approach Demo:
https://github.com/CommunityToolkit/Maui/assets/5452331/d6bbff50-656a-489f-a53f-f54357c0d9fa

@MAUIoxo
Copy link

MAUIoxo commented May 13, 2024

As discussed in #1864, I used a simple Popup defined like this and created it in Code-behind as can be seen below. It was displayed via Application.Current?.MainPage?.ShowPopup(myPopup);.

Hint: I used iPhone with latest iOS and currently 17.2

<?xml version="1.0" encoding="utf-8" ?>
<toolkit:Popup xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
               xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
               xmlns:loc="clr-namespace:LocalizationResourceManager.Maui;assembly=LocalizationResourceManager.Maui"
               xmlns:toolkit="clr-namespace:CommunityToolkit.Maui.Views;assembly=CommunityToolkit.Maui"
               x:Class="OptimizerApp.Pages.Views.Controls.CustomActivityIndicator.CustomCalculationActivityIndicator"
               CanBeDismissedByTappingOutsideOfPopup="False">

    <VerticalStackLayout Background="White">
        
        <!-- Inner StackLayout needed to have an easy nice border with the Margin -->
        <VerticalStackLayout Background="White" Margin="30" HorizontalOptions="Center" VerticalOptions="Center">
            <ActivityIndicator IsRunning="True"/>
            <Label Text="{loc:Translate ActivityIndicator_CalculatingLabel}" FontSize="13" Margin="0, 10, 0, 0"/>
        </VerticalStackLayout>
        
    </VerticalStackLayout>    
    
</toolkit:Popup>
using CommunityToolkit.Maui.Views;

namespace OptimizerApp.Pages.Views.Controls.CustomActivityIndicator;

public partial class CustomCalculationActivityIndicator : Popup
{
    public CustomCalculationActivityIndicator()
    {
        InitializeComponent();
    }
}

And in my ViewModel, I created a new instance of it and displayed it via:

var myPopup = new CustomCalculationActivityIndicator();
…
Application.Current?.MainPage?.ShowPopup(myPopup);

As I changed to my own implementation of a Popup using a ContentPage, I can't provide a minimal reproducible example. But, you can try with the above setup and then switch from Portrait to Landscape. When I did this with newer MAUI NIghtly-Builds, I could reproduce it just by switching the orientation

This resulted in Exceptions in SetShadowView(...) with a NullReferenceException

326288575-f2776053-64ce-4dcc-bb03-870f1436c6f7

326288654-30c7d215-a904-4bbc-8758-f3c15a1a9345

@cat0363
Copy link
Contributor

cat0363 commented May 14, 2024

I changed the simulator on iOS 17 and verified whether the problem occurred.

iPhone 15 : OK
iPhone 15 Plus : NG
iPhone 15 Pro : OK
iPhone 15 Pro Max : NG

This issue occurred in Plus and Pro Max simulators.
This issue occurred with the simulator on iPhone 12 and later versions of Plus and Pro Max.
This issue also occurred on iPhone 11 or lower versions.

In addition, in versions earlier than Community Toolkit 8.0.0, the crash does not occur when the device is rotated, but the Popup becomes full screen without the background color becoming transparent.

iPhone.12.Pro.Max.iOS.14.5.2024-05-14.16-25-19.mp4

By applying PR #1615, the above issue will be resolved on iPhone 12 and later simulators other than Plus and ProMax.
However, other simulators will run into issue with PR #1844. Even if you fix it so that the Null Reference Exception does not occur, the problem will not be resolved.

Even if you specify Start in the Vertical Options and Horizontal Options of Popup, if you rotate the device orientation, it will be displayed full screen and centered.

iPhone.12.Pro.Max.iOS.14.5.2024-05-14.16-42-21.mp4

Even if you explicitly specify the Popup position in SourceRect, it will not be reflected if the device orientation is Landscape.

[src\CommunityToolkit.Maui.Core\Views\Popup\PopupExtensions.macios.cs]

mauiPopup.PopoverPresentationController.SourceRect = new CGRect(originX, originY, 0, 0);

It is unclear why the position of Popup is not reflected. It is also unclear why the Popup size covers the entire screen when the device orientation is Landscape...

We need to resolve a Popup size and position issue that occurs when I change the device orientation to Landscape.

Additional Information:
I forgot to mention that this problem also occurs in versions earlier than iOS 17.
I don't know if the same problem as the Simulator will occur on the actual iPhone Plus and iPhone Pro Max.

@MAUIoxo
Copy link

MAUIoxo commented May 14, 2024

Thanks for finding that out! I recognize parts of it also in previous Simulator versions or .NET MAUI versions! I also used a Pro Max here. The FullScreen issue in previous versions when rotating is also very familiar to me!

@cat0363
Copy link
Contributor

cat0363 commented May 14, 2024

@MAUIoxo , Thank you for providing the information.
Does anyone know if the same problem occurs on a real device?

@MAUIoxo
Copy link

MAUIoxo commented May 15, 2024

I have an iPhone 14 Pro Max and I had a crash of my App with this Bug when changing orientation! Also the problem regarding full screen in previous versions is well known to me on a real device

@cat0363
Copy link
Contributor

cat0363 commented May 16, 2024

@MAUIoxo , Thank you for reporting the problem on a real device.
I have found the model to reproduce the problem, but unfortunately I do not have a solution at this time.

@MAUIoxo
Copy link

MAUIoxo commented May 16, 2024

Well, not bad for me, I created my own Popup with a simple ContentPage which is working fine in my environment

@cat0363
Copy link
Contributor

cat0363 commented May 16, 2024

I found the solution for this issue. I will create a PR after tomorrow.
Just adding a few lines of code will solve problem.

@MAUIoxo
Copy link

MAUIoxo commented May 16, 2024

Wow, that was fast - do you want to tell us about the problem you found?

@cat0363 cat0363 mentioned this issue May 17, 2024
6 tasks
@cat0363
Copy link
Contributor

cat0363 commented May 17, 2024

@MAUIoxo , I just created a PR, so please take a look at it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/views Issue/Discussion/PR that has to do with Views bug Something isn't working unverified
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants