How can I create a view like background view of UIActionSheet in iOS?
The view below the UIVisualEffectView
(the one with the red arrow in the question) contains a layer which has a CAFilter "overlayBlendMode" (private class) set as compositing filter. It's applied over the layer behind it. In the screenshot below I changed the background colour of the _UIDimmingKnockoutBackdropView to green and the filter is applied over that.
Private method
To create the same effect, you need to put a white opaque view below the UIVisualEffectView
and apply a CAFilter to it:
CAFilter *filter = [CAFilter filterWithName:@"overlayBlendMode"];
[[contentView layer] setCompositingFilter:filter];
Since CAFilter is private, we need a header for it:
#import <Foundation/Foundation.h>
@interface CAFilter : NSObject <NSCoding, NSCopying, NSMutableCopying> {
void * _attr;
void * _cache;
unsigned int _flags;
NSString * _name;
unsigned int _type;
}
@property BOOL cachesInputImage;
@property (getter=isEnabled) BOOL enabled;
@property (copy) NSString *name;
@property (readonly) NSString *type;
// Image: /System/Library/Frameworks/QuartzCore.framework/QuartzCore
+ (void)CAMLParserStartElement:(id)arg1;
+ (BOOL)automaticallyNotifiesObserversForKey:(id)arg1;
+ (id)filterTypes;
+ (id)filterWithName:(id)arg1;
+ (id)filterWithType:(id)arg1;
- (void)CAMLParser:(id)arg1 setValue:(id)arg2 forKey:(id)arg3;
- (id)CAMLTypeForKey:(id)arg1;
- (struct Object { int (**x1)(); struct Atomic { struct { int x_1_2_1; } x_2_1_1; } x2; }*)CA_copyRenderValue;
- (BOOL)cachesInputImage;
- (id)copyWithZone:(struct _NSZone { }*)arg1;
- (void)dealloc;
- (BOOL)enabled;
- (void)encodeWithCAMLWriter:(id)arg1;
- (void)encodeWithCoder:(id)arg1;
- (id)initWithCoder:(id)arg1;
- (id)initWithName:(id)arg1;
- (id)initWithType:(id)arg1;
- (BOOL)isEnabled;
- (id)mutableCopyWithZone:(struct _NSZone { }*)arg1;
- (id)name;
- (void)setCachesInputImage:(BOOL)arg1;
- (void)setDefaults;
- (void)setEnabled:(BOOL)arg1;
- (void)setName:(id)arg1;
- (void)setValue:(id)arg1 forKey:(id)arg2;
- (id)type;
- (id)valueForKey:(id)arg1;
// Image: /System/Library/PrivateFrameworks/PhotosUICore.framework/PhotosUICore
+ (id)px_filterWithPXCompositingFilterType:(int)arg1;
@end
The result:
The view I've created prints the same compositing filter as Apple used:
Workaround
As a workaround you can create an image of the view on the background and a image of the white view and apply the overlay blend mode with that. This results in a comparable result as using the private class.
Example:
UIImage *image = [self imageForView:contentView];
UIView *superview = [contentView superview];
[contentView setHidden:YES];
UIImage *backgroundImage = [self imageForView:superview];
CGImageRef imageRef = CGImageCreateWithImageInRect([backgroundImage CGImage], [contentView frame]);
backgroundImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
[contentView setHidden:NO];
CIFilter *filter = [CIFilter filterWithName:@"CIOverlayBlendMode"];
[filter setValue:[backgroundImage CIImage] forKey:kCIInputImageKey];
[filter setValue:[image CIImage] forKey:kCIInputBackgroundImageKey];
[contentView setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageWithCIImage:[filter outputImage]]]];
}
- (UIImage *)imageForView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions([view bounds].size, [view isOpaque], 1.0);
[[view layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
Result: