Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
794 views
in Technique[技术] by (71.8m points)

ios - Title for selected segment in UISegmentedControl disappears when taking a screenshot programmatically

I have a UISegmentedControl added to a UIToolbar:

UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
toolbar.barTintColor = [UIColor colorWithRed:0.97 green:0.97 blue:0.97 alpha:1.0];
[self.view addSubview:toolbar];

UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:@[@"Last week", @"Last month", @"All time"];
[segmentedControl setSelectedSegmentIndex:0];
[toolbar addSubview:segmentedControl];

The toolbar is then added to a view controller. When I take a screenshot of the view controller, something weird happens. The title of the selected segment disappears.

The screenshot is being taken using:

UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

Rendered view: enter image description here

Screenshot: enter image description here

Where to start looking?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I've replaced

[self.view.layer renderInContext:UIGraphicsGetCurrentContext()]; 

With

[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];

And now it works as expected. Still don't understand why that happens though.

Note that drawViewHierarchyInRect:afterScreenUpdates: is iOS 7 only.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...