Domanda

In iOS7, a popover causes the rest of the screen to be dimmed. As per the Apple docs:

The popover content is layered on top of your existing content and the background is dimmed automatically.

This is nice in most cases, but I have an app where the screen rearranges itself when the popover opens and stays responsive, so the dimming only causes confusion. Anyone knows if dimming can be disabled?

È stato utile?

Soluzione

Doesn’t look like there’s anything in the API to support that—you can set the passthroughViews property to allow other views to be interacted with while the popover’s open, but that doesn’t affect the dimming. You may have to roll your own popover implementation or find a third-party version.

Altri suggerimenti

I can suggest you a custom control which is really nice work by its author. It do not dim the background. Further it has many customization.

Here is the github link for WYPopoverController

For me at works like this. I just work through all subviews if key window view, find _UIMirrorNinePatchView. _UIMirrorNinePatchView is apple class for that has four image views, these image views create the dimming background for 4 directions of PopOverPresentationController. More specifically you can look at this if you use view hierarchy debugger. So I walk through the array of these UIImageView and set UIImage to nil. This code paste in viewWillAppear of your destination controller(popOverContoller).

NSArray<UIView *> *arrayOfSubviews = [UIApplication sharedApplication].keyWindow.subviews.lastObject.subviews;
for (int i = 0; i < arrayOfSubviews.count; i++) {
if ([NSStringFromClass(arrayOfSubviews[i].class) isEqualToString:@"_UIMirrorNinePatchView"]) {
        arrayOfSubviews[i].backgroundColor = [UIColor clearColor];
        NSArray<UIImageView *> *arrayOfImageViews = arrayOfSubviews[i].subviews;
            for (int j = 0; j < arrayOfImageViews.count; j++) {
                arrayOfImageViews[j].image = nil;
            }
    }
}

In whole my UIPopOverController looks like this The view And in view debugger, it looks so View debugger

So as you can understand, setting UIImage to nil will remove this dimming view.

This is the swift version to remove the dimming of UIPopoverController

let allSubViews: [UIView] = (UIApplication.shared.keyWindow?.subviews.last?.subviews)!

        for index in 0...allSubViews.count - 1 {
            allSubViews[index].removeFromSuperview()
                        if NSStringFromClass(allSubViews[index].classForCoder) == "_UIMirrorNinePatchView"
                        {
                            allSubViews[index].backgroundColor = UIColor.clear

                            let arrayofImages = allSubViews[index].subviews as! [UIImageView]

                            for imageIndex in 0...arrayofImages.count - 1 {
                                arrayofImages[imageIndex].image = nil
                            }
                        }
        }

You can prevent the dimming by setting the UIPopoverBackgroundView for your popover and setting the background to be transparent for the background view.

You will need to re-implement how the popover draws the arrows, but you can find plenty of examples for that online.

Updated to work in iOS 13 with Swift 4

    guard let transitionSubviews = UIApplication.shared.keyWindow?.subviews.last?.subviews else { return }

    func findViews<T>(inView view: UIView, subclassOf targetType: T.Type) -> [T] {
        return recursiveSubviews(inView: view).compactMap { $0 as? T }
    }

    func recursiveSubviews(inView view: UIView) -> [UIView] {
        return view.subviews + view.subviews.flatMap { recursiveSubviews(inView: $0) }
    }

    for view in transitionSubviews {
        view.backgroundColor = UIColor.clear
        for imageView in findViews(inView: view, subclassOf: UIImageView.self) {
            imageView.image = nil
        }
    }

If you choose to implement your custom UIPopoverBackgroundView, you can set the layer background to be clear - layer.shadowColor = UIColor.clearColor().CGColor. However this will eliminate the dim and the shadow completely so you will have to put a border around the controller

[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];

This solved my problem with navigation bar dimming effect while transiting.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top