以编程方式截取屏幕截图时,UISegmentedControl中所选段的标题消失 [英] Title for selected segment in UISegmentedControl disappears when taking a screenshot programatically
问题描述
我将 UISegmentedControl
添加到 UIToolbar
:
UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:toolbarFrame];
toolbar.barStyle = UIBarStyleBlack;
toolbar.translucent = YES;
toolbar.barTintColor = [UIColor colorWithRed:0.97绿色:0.97蓝色:0.97 alpha:1.0];
[self.view addSubview:toolbar];
UISegmentedControl * segmentedControl = [[UISegmentedControl alloc] initWithItems:@ [@上周,@上个月,@所有时间];
[segmentedControl setSelectedSegmentIndex:0];
[toolbar addSubview:segmentedControl];
然后将工具栏添加到视图控制器中。当我拍摄视图控制器的屏幕截图时,会发生一些奇怪的事情。所选片段的标题消失。
屏幕截图使用:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size,YES,[[UIScreen mainScreen] scale]);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
渲染视图:
屏幕截图:
任何有关从哪里开始寻找的指示都将受到赞赏。
我已经更换了
[self.view .layer renderInContext:UIGraphicsGetCurrentContext()];
使用
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
现在它按预期工作。仍然不明白为什么会发生这种情况。
请注意 drawViewHierarchyInRect:afterScreenUpdates:
仅限iOS 7。 / p>
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:
Screenshot:
Any pointers on where to start looking will be appreciated.
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.
这篇关于以编程方式截取屏幕截图时,UISegmentedControl中所选段的标题消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!