【发布时间】:2020-03-27 19:28:50
【问题描述】:
我的场景:
我有一个 UIViewController 和一个 UIButton 和一个 UITextField 作为第一响应者,当单击 UIButton 时,我使用自定义转换呈现另一个视图控制器(这个自定义转换委托给 UIPreentationController它将“暗淡”视图(alpha 0.5 的黑色背景)添加到 containerView 边界,并在底部添加另一个视图,高度为 200 点以显示消息。整个显示警报。
我尝试了很多方法来保持键盘在此转换期间/之后被关闭,但没有任何运气,它总是消失。现在我想知道是否可以在文本字段作为响应者之后截取包括键盘在内的屏幕截图?无需请求额外的应用程序功能。然后我可以在过渡期间将此图像添加到背景中,并保持“冻结”的屏幕外观。
感谢您的任何指导
编辑:
我找到了以下代码,其中包含我想要的代码。但是,键盘背景正在丢失。有人知道为什么吗?
func screenshot() -> UIImage {
let imageSize = UIScreen.main.bounds.size as CGSize;
UIGraphicsBeginImageContextWithOptions(imageSize, true, 0)
let context = UIGraphicsGetCurrentContext()
for obj : AnyObject in UIApplication.shared.windows {
if let window = obj as? UIWindow {
print(window)
if window.responds(to: #selector(getter: UIWindow.screen)) || window.screen == UIScreen.main {
print(window)
// so we must first apply the layer's geometry to the graphics context
context!.saveGState();
// Center the context around the window's anchor point
context!.translateBy(x: window.center.x, y: window.center
.y);
// Apply the window's transform about the anchor point
context!.concatenate(window.transform);
// Offset by the portion of the bounds left of and above the anchor point
context!.translateBy(x: -window.bounds.size.width * window.layer.anchorPoint.x,
y: -window.bounds.size.height * window.layer.anchorPoint.y);
// Render the layer hierarchy to the current context
window.layer.render(in: context!)
// Restore the context
context!.restoreGState();
}
}
}
let image = UIGraphicsGetImageFromCurrentImageContext();
return image!
}
【问题讨论】: