Ios 当用户单击UITextView并显示键盘时,如何使背景变暗/着色?
目前,我可以在单击UITextView的内部/外部时显示/隐藏键盘。我希望在键盘出现时将背景颜色调暗,然后在键盘消失时使背景恢复正常,从而使效果更好 我被告知这种行为可能被称为“聚焦”或“模式阴影覆盖”,但一直找不到符合我目标的教程或图像。这正是我想要实现的目标:在为一篇新Instagram帖子写标题时,背景会变得更暗Ios 当用户单击UITextView并显示键盘时,如何使背景变暗/着色?,ios,swift,uitextview,Ios,Swift,Uitextview,目前,我可以在单击UITextView的内部/外部时显示/隐藏键盘。我希望在键盘出现时将背景颜色调暗,然后在键盘消失时使背景恢复正常,从而使效果更好 我被告知这种行为可能被称为“聚焦”或“模式阴影覆盖”,但一直找不到符合我目标的教程或图像。这正是我想要实现的目标:在为一篇新Instagram帖子写标题时,背景会变得更暗 如何在Swift中实现此行为 在iOS编程中,这种行为叫什么 多谢各位。[:首先回答你的两个问题: 有许多方法可以实现覆盖 您可以添加一个UIView作为子视图,以文本视图的垂
多谢各位。[:首先回答你的两个问题:
- 您可以添加一个UIView作为子视图,以文本视图的垂直间距为约束条件,对齐self.view的底部,并在显示/取消键盘时更改其alpha
- 您可以在viewcontroller的self.view中添加一个MaskView。问题是,当应用遮罩时,它会将所有其他区域变为黑色(当然,您可以更改颜色),并且只有在遮罩框中设置的部分才会是您建议的颜色(黑色,带有0.5 alpha)
var overlay: UIView = UIView(frame: CGRect.zero)
之后,我在viewDidLoad中为keyboardWillShow和keyboardWillHide添加通知观察员
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
并添加相应的选择器来处理通知
@objc func keyboardWillShow(notification: NSNotification)
@objc func keyboardWillHide(notification: NSNotification)
在keyboardWillShow中,我获取键盘框架以获取键盘高度
在那之后,我通过获得屏幕的高度并减去导航栏高度、文本视图高度和添加到文本视图顶部的任何边距来计算覆盖的高度
然后,我初始化我的覆盖变量,从文本视图的正下方给它一个Y位置。最初,我将它的颜色设置为UIColor。清除将它作为子视图添加到self.view,然后用0.5 alpha和0.5持续时间的动画将它的颜色更改为黑色
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let overlayHeight = UIScreen.main.bounds.height - heightOfNavigationBar - keyboardSize.height - textView.frame.size.height - topConstraintofTextView(if any)
let overlayFrame = CGRect(x: 0, y: textView.frame.size.height + textView.frame.origin.y, width: UIScreen.main.bounds.width, height: overlayHeight)
self.overlay = UIView(frame: overlayFrame)
self.overlay.backgroundColor = .clear
self.view.addSubview(overlay)
UIView.animate(withDuration: 0.5, animations: {
self.overlay.backgroundColor = UIColor.black.withAlphaComponent(0.5)
})
}
}
之后,在keyboardWillHide中,我通过一点动画将覆盖的alpha更改为0,一旦结束,我就从superView中删除覆盖
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.5, animations: {
self.overlay.backgroundColor = UIColor.black.withAlphaComponent(0)
}, completion: { (completed) in
self.overlay.removeFromSuperview()
})
self.overlay.removeFromSuperview()
}
我会在viewController的触摸中执行self.view.endEditing(true)
关闭键盘,但这取决于您如何关闭键盘
这是它的样子
希望有帮助!首先回答您的两个问题:
- 您可以添加一个UIView作为子视图,以文本视图的垂直间距为约束条件,对齐self.view的底部,并在显示/取消键盘时更改其alpha
- 您可以在viewcontroller的self.view中添加一个MaskView。问题是,当应用遮罩时,它会将所有其他区域变为黑色(当然,您可以更改颜色),并且只有在遮罩框中设置的部分才会是您建议的颜色(黑色,带有0.5 alpha)
var overlay: UIView = UIView(frame: CGRect.zero)
之后,我在viewDidLoad中为keyboardWillShow和keyboardWillHide添加通知观察员
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
并添加相应的选择器来处理通知
@objc func keyboardWillShow(notification: NSNotification)
@objc func keyboardWillHide(notification: NSNotification)
在keyboardWillShow中,我获取键盘框架以获取键盘高度
在那之后,我通过获得屏幕的高度并减去导航栏高度、文本视图高度和添加到文本视图顶部的任何边距来计算覆盖的高度
然后,我初始化我的覆盖变量,从文本视图的正下方给它一个Y位置。最初,我将它的颜色设置为UIColor。清除将它作为子视图添加到self.view,然后用0.5 alpha和0.5持续时间的动画将它的颜色更改为黑色
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let overlayHeight = UIScreen.main.bounds.height - heightOfNavigationBar - keyboardSize.height - textView.frame.size.height - topConstraintofTextView(if any)
let overlayFrame = CGRect(x: 0, y: textView.frame.size.height + textView.frame.origin.y, width: UIScreen.main.bounds.width, height: overlayHeight)
self.overlay = UIView(frame: overlayFrame)
self.overlay.backgroundColor = .clear
self.view.addSubview(overlay)
UIView.animate(withDuration: 0.5, animations: {
self.overlay.backgroundColor = UIColor.black.withAlphaComponent(0.5)
})
}
}
之后,在keyboardWillHide中,我通过一点动画将覆盖的alpha更改为0,一旦结束,我就从superView中删除覆盖
@objc func keyboardWillHide(notification: NSNotification) {
UIView.animate(withDuration: 0.5, animations: {
self.overlay.backgroundColor = UIColor.black.withAlphaComponent(0)
}, completion: { (completed) in
self.overlay.removeFromSuperview()
})
self.overlay.removeFromSuperview()
}
我会在viewController的触摸中执行self.view.endEditing(true)
关闭键盘,但这取决于您如何关闭键盘
这是它的样子
希望能有所帮助!看看在UITableView中,您希望只有一个单元格位于前景,其余的单元格在后面更暗,您会在UITableView中使用什么方法,您希望只有一个单元格位于前景,其余的单元格在后面更暗?