UISegmentedControll in UINavigationBar weird iOS screenshot

UISegmentedControlInside me, a UINavigationBarlooks like this:

enter image description here

Here you can see how it is created in the Storyboard:

enter image description here

The problem is that when I create a software screenshot to share it, I got the UISegmentedControlselected tab without the text, for example here:

enter image description here

As far as I know, it’s normal that the status bar is not displayed, and also that the Share button is displayed as selected, because it is actually selected at the time of taking the screenshot, but has no idea what is happening with UISegmentedControlany idea?

PS: I can use the screen capture code, but this is a pretty simple standard code.

UPDATE

This is the screenshot code I'm using:

- (UIImage*)screenshot
{
    // Create a graphics context with the target size
    // On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
    // On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
    CGSize imageSize = [[UIScreen mainScreen] bounds].size;
    if (NULL != UIGraphicsBeginImageContextWithOptions)
        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
    else
        UIGraphicsBeginImageContext(imageSize);

    CGContextRef context = UIGraphicsGetCurrentContext();

    // Iterate over every window from back to front
    for (UIWindow *window in [[UIApplication sharedApplication] windows])
    {
        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])
        {
            // -renderInContext: renders in the coordinate space of the layer,
            // so we must first apply the layer geometry to the graphics context
            CGContextSaveGState(context);
            // Center the context around the window anchor point
            CGContextTranslateCTM(context, [window center].x, [window center].y);
            // Apply the window transform about the anchor point
            CGContextConcatCTM(context, [window transform]);
            // Offset by the portion of the bounds left of and above the anchor point
            CGContextTranslateCTM(context,
                                  -[window bounds].size.width * [[window layer] anchorPoint].x,
                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            // Render the layer hierarchy to the current context
            [[window layer] renderInContext:context];

            // Restore the context
            CGContextRestoreGState(context);
        }
    }

    // Retrieve the screenshot image
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return image;
}

UPDATE 2

, : https://github.com/apascual/APSegmentedControlExample

+4
2

iOS 7? , screenShot api? , afterScreenUpdates ?

- (UIImage *) getScreenShotForView:(UIView *)view {
    UIGraphicsBeginImageContext(view.bounds.size);

    // I think this is what was used pre iOS 7.x
    //[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];

    // iOS 7.x -->
    [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; // Set To YES
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}

@LeoNatan

- (UIImage *) getScreenShotForView:(UIView *)view {
    UIGraphicsBeginImageContext(view.bounds.size);

    // iOS 7.x -->
    [[[[UIApplication sharedApplication] windows] objectAtIndex:0] drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; // Set To YES
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;
}
+8

:

[[window layer] renderInContext:context];

[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES]; // also tried with "inRact:window.bounds";

- drawViewHierarchyInRect:afterScreenUpdates: iOS 7.

, , ...

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
+2

Source: https://habr.com/ru/post/1531664/


All Articles