【问题标题】:Title for selected segment in UISegmentedControl disappears when taking a screenshot programmatically以编程方式截取屏幕截图时,UISegmentedControl 中选定段的标题消失
【发布时间】:2014-02-18 17:35:16
【问题描述】:

我将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];

然后将工具栏添加到视图控制器。当我截取视图控制器的屏幕截图时,发生了一些奇怪的事情。所选片段的标题消失。

屏幕截图正在使用:

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

渲染视图:

截图:

从哪里开始寻找?

【问题讨论】:

    标签: ios iphone ios7 uisegmentedcontrol


    【解决方案1】:

    我换了

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

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

    现在它按预期工作了。不过还是不明白为什么会这样。

    请注意,drawViewHierarchyInRect:afterScreenUpdates: 仅适用于 iOS 7。

    【讨论】:

    • 有什么想法吗? drawViewHierarchyInRect:afterScreenUpdates:YES 对我们还有其他一些问题...
    • drawViewHierarchyInRect:afterScreenUpdates: 给您带来了什么问题?
    • 有时比较慢
    • iOS8 上的 drawViewHierarchyInRect 导致白色图像
    • 我尝试替换上面答案中提到的行,但是我仍然面临同样的问题。 SegmentedControl 的文字在截图中消失了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-31
    • 2012-01-31
    • 1970-01-01
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多