如何禁止在 uipopoverController 后面的视图上进行交互

本文关键字:视图 交互 uipopoverController 何禁止 禁止 | 更新日期: 2023-09-27 18:36:22

我有一个正在使用的UIPopoverController,我有两个按钮,每个按钮在单击时都会显示一个弹出窗口。但是,我不希望同时显示弹出窗口 - 这意味着我不希望用户能够按下一个按钮,并且在显示弹出窗口时能够按下另一个按钮。似乎我已经尝试了一切 - 禁用按钮上的用户交互,隐藏弹出窗口后面的视图,使用弹出窗口的直通视图等等。都行不通!禁用用户交互似乎在大多数情况下都有效,但随后停止禁止用户与按钮交互并导致应用程序崩溃......

popupView.PassthroughViews = new UIView[]{this.View.Superview, this.View, this.Gray}; //gray is another view that sits under the view that calls the popup
this.View.UserInteractionEnabled = false;
this.PositiveMeterBtn.UserInteractionEnabled = false; 
this.View.Hidden = true;

我的UIPopoverController是在类级别声明的,我什至做了这样的代码:

if(popupView != null)
    return;

我仍然收到多个弹出窗口。我正在使用单点触控/xamarin - 这是 xamarin 的错误还是 ios 问题?我是否以正确的方式处理此问题?

如何禁止在 uipopoverController 后面的视图上进行交互

我以前没有使用过 Xamarin,但在原生 Objective-C 中对我有用的是

[controller setModalInPopover:YES];

其中controller是弹出框中显示的视图控制器。

从 UIViewController 类参考:

@property(nonatomic, readwrite, getter=isModalInPopover) BOOL modalInPopover

此属性的默认值为 NO。将其设置为 YES 会导致拥有弹出框控制器在显示此视图控制器时禁止在此视图控制器之外进行交互。

您可以将弹出框设为模态,但如果它不包含本应为模态的内容,则不应阻止用户。

通常,更好的选择是创建两个帮助程序方法,并将它们放置在应用委托中。这些方法负责在显示现有弹出框时关闭另一个弹出框。这样,您将拥有最大的UIPopoverController,而不必担心被解雇。

/// <summary>
/// Shows a popover.
/// </summary>
/// <param name='controllerToShow'>the controller to show in the popover</param>
/// <param name='showFromRect'>the rectangle to present the popover from. Not used if showFromItem is specified.</param>
/// <param name='showInView'>the view the popover is hosted in</param>
/// <param name='showFromItem'>the bar button item the popover gets presented from.</param>
/// <param name='popoverContentSize'>the content size of the popover</param>
/// <param name='animated'>If set to <c>true</c>, animated the popover</param>
/// <param name='arrowDirection'>the allowed arrow directions</param>
/// <param name='onDismiss'>callback if the popover gets dismissed. Careful that the object that owns the callback doesn't outlive the popover controller to prevent uncollectable memory.</param>
public static void ShowPopover(UIViewController controllerToShow, RectangleF showFromRect, UIView showInView, UIBarButtonItem showFromItem, SizeF popoverContentSize, bool animated = true, UIPopoverArrowDirection arrowDirection = UIPopoverArrowDirection.Any, EventHandler onDismiss = null)
{
    if(AppDelegateBase.popoverController != null)
    {
        AppDelegateBase.DismissPopover(false);
    }
    if(showFromItem == null && showFromRect.IsEmpty)
    {
        // Nothing to attach the popover to.
        return;
    }
    popoverController = new UIPopoverController(controllerToShow);
    if(!popoverContentSize.IsEmpty)
    {
        popoverController.SetPopoverContentSize(popoverContentSize, false);
    }
    if(onDismiss != null)
    {
        popoverController.DidDismiss += onDismiss;
    }
    // Send a notification that a popover will be presented.
    NSNotificationCenter.DefaultCenter.PostNotificationName("WillPresentPopover", popoverController);
    if(showFromItem != null)
    {
        popoverController.PresentFromBarButtonItem(showFromItem, arrowDirection, animated);
    }
    else 
    {
        popoverController.PresentFromRect(showFromRect, showInView, arrowDirection, animated );
    }
}
/// <summary>
/// Dismisses the popover presented using ShowPopover().
/// </summary>
/// <param name='animated'>If set to <c>true</c>, animates the dismissal</param>
public static void DismissPopover(bool animated = false)
{
    if(popoverController != null)
    {
        popoverController.Dismiss(animated);
    }
    AppDelegateBase.popoverController = null;
}
private static UIPopoverController popoverController;

您可以尝试的一件事是使用该方法

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)identifier sender:(id)sender

该方法中,请检查您的弹出框视图控制器之一是否在屏幕上。

if (popupView.view.window) {
    return NO;
} else {
    return YES;
}