我将 UISegmentedControl
添加到 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];
然后将工具栏添加到 View Controller 中。当我截取 View Controller 的屏幕截图时,发生了一些奇怪的事情。所选片段的标题消失。
屏幕截图是使用以下方式拍摄的:
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, YES, [[UIScreen mainScreen] scale]);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
渲染 View :
截图:
从哪里开始寻找?
最佳答案
我已经替换了
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
与
[self.view drawViewHierarchyInRect:self.view.bounds afterScreenUpdates:YES];
现在它按预期工作了。但还是不明白为什么会发生这种情况。
请注意,drawViewHierarchyInRect:afterScreenUpdates:
仅适用于 iOS 7。
关于ios - 以编程方式截取屏幕截图时,UISegmentedControl 中选定段的标题消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21388335/