ios - 以编程方式截取屏幕截图时,UISegmentedControl 中选定段的标题消失

标签 ios iphone ios7 uisegmentedcontrol

我将 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 : enter image description here

截图: enter image description here

从哪里开始寻找?

最佳答案

我已经替换了

[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/

相关文章:

iphone - Obj-C 中 [object variable] 和 object.variable 的区别?

ios - 上传到s3,iOS成功回调

objective-c - 如何知道 NSURLSessionDataTask 的 for 循环何时完成

objective-c - 游戏开发中的日志统计

ios - 如何在 iPhone 应用程序中集成 AdMob

ios - “@property(非原子,强)”和“@property”之间的区别

iphone - 在数字键盘上显示丢弃和完成按钮

iphone - 有没有办法将 iOS 应用程序转换为 Mac OS X 应用程序?

ios - 如何检查UITableViewCellAccessoryDe​​tailButton的可用性?

ios - 是否可以在 UIImagePickerController 中以编程方式调用 didFinishPickingMediaWithInfo