我在找出键盘出现时如何移动UILabel时遇到问题。Swift:当键盘出现时向上移动UILabel
目前,该标签从顶部位于大约140像素,当,当我使用模拟7 iPhone
但使用较小的iPhone 5时出现的键盘,我有这个问题坐镇完美中心。
其中键盘重叠的UILabel。
我想要做的是当键盘出现时将UILabel居中。
大多数教程都展示了如何移动UITextField,但我试图对UILabel应用相同的方面,但失败了。
我在找出键盘出现时如何移动UILabel时遇到问题。Swift:当键盘出现时向上移动UILabel
目前,该标签从顶部位于大约140像素,当,当我使用模拟7 iPhone
但使用较小的iPhone 5时出现的键盘,我有这个问题坐镇完美中心。
其中键盘重叠的UILabel。
我想要做的是当键盘出现时将UILabel居中。
大多数教程都展示了如何移动UITextField,但我试图对UILabel应用相同的方面,但失败了。
当键盘出现时移动控件的一种可能的解决方案是向控件添加底部约束并为这些约束定义出口。
@IBOutlet weak var controlBottomConstraint: NSLayoutConstraint!
在viewDidLoad
注册keyboardWillShow
方法中,当键盘出现像接收通知:
NotificationCenter.default.addObserver(self, selector: #selector(MyViewController.keyboardWillShow(_:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
然后在keyboardWillShow
方法更新约束(controlBottomConstraint
)所示:
func keyboardWillShow(_ notification:Notification) {
...
let userInfo:NSDictionary = (notification as NSNotification).userInfo! as NSDictionary
let keyboardFrame:NSValue = userInfo.value(forKey: UIKeyboardFrameEndUserInfoKey) as! NSValue
let keyboardRectangle = keyboardFrame.cgRectValue
let keyboardHeight = keyboardRectangle.height
// controlBottomConstraint outlet to the control you want to move up
controlBottomConstraint.constant = keyboardHeight + 8
}
它也适用于旋转设备。
@daanyyaal如果这个答案解决了你的问题,我会很高兴你接受它。 –
此答案是最好的。我使用了topConstraint,而不是使用bottomConstraint。但是,现在我需要弄清楚在键盘出现时,如何根据不同的设备对UILabel进行居中。 – daanyyaal
如果您正在进行约束,只需将乘数更改为0.5即可。如果您正在制作相框,请将相框的y位置(yourLabel.frame.origin.y
)设置为self.view.frame.height/2
。
尝试这个办法:一个延伸和补充通知观察家
class ViewController: UIViewController {
var keyboardOnScreen = false
@IBOutlet weak var searchBar: UISearchBar!
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name:.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: .UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidShow), name: .UIKeyboardDidShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardDidHide), name: .UIKeyboardDidHide, object: nil)
searchBar.delegate = self
}
}
extension ViewController: UISearchBarDelegate {
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
func keyboardWillShow(_ notification: Notification) {
if !keyboardOnScreen {
view.frame.origin.y -= keyboardHeight(notification) }
}
func keyboardWillHide(_ notification: Notification) {
if keyboardOnScreen {
view.frame.origin.y += keyboardHeight(notification)
}
}
func keyboardDidShow(_ notification: Notification) {
keyboardOnScreen = true
}
func keyboardDidHide(_ notification: Notification) {
keyboardOnScreen = false
}
private func keyboardHeight(_ notification: Notification) -> CGFloat {
let userInfo = (notification as NSNotification).userInfo
let keyboardSize = userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardSize.cgRectValue.height
}
private func resignIfFirstResponder(_ textField: UITextField) {
if textField.isFirstResponder {
textField.resignFirstResponder()
}
}
@IBAction func userDidTapView(_ sender: AnyObject) {
resignIfFirstResponder(textField)
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
}
第一的ViewController
import UIKit
class ViewController: UIViewController , UITableViewDelegate ,UITableViewDataSource, UITextViewDelegate {
@IBOutlet weak var ViewSend: UIView!
@IBOutlet weak var txtMessage: UITextView!
@IBOutlet weak var ChatTable: UITableView!
var chatArray:[String] = []
var collect:String!
override func viewDidLoad()
{
super.viewDidLoad()
txtMessage.delegate = self
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillShow), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(ViewController.keyboardWillHide), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
print(self.ViewSend.frame.origin.y)
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
{
return chatArray.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "CellDemo") as! chatlableTableViewCell
let obj = chatArray[indexPath.row]
cell.lblchat.text = obj
cell.lblchat.sizeToFit()
cell.lblchat.layer.cornerRadius = 2
cell.lblchat.clipsToBounds = true
return cell
}
@IBAction func btnSendPresssed(_ sender: UIButton)
{
if txtMessage.text == ""
{
}
else
{
collect = txtMessage.text
chatArray.append(collect)
}
ChatTable.reloadData()
txtMessage.text = ""
}
func calcheight(strin:String) -> CGFloat
{
let label = UILabel(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.size.width, height: CGFloat.greatestFiniteMagnitude))
label.numberOfLines = 0
label.text = strin
label.sizeToFit()
return label.frame.height + 2
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
let height = self.calcheight(strin: String(describing: chatArray[indexPath.row]))
return height
}
func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
{
if self.ViewSend.frame.origin.y != keyboardSize.height
{
self.ViewSend.frame.origin.y -= keyboardSize.height
self.ChatTable.frame.size.height -= keyboardSize.height
}
}
}
func keyboardWillHide(notification: NSNotification)
{
if let keyboardSize = (notification.userInfo?[UIKeyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue
{
self.ViewSend.frame.origin.y += keyboardSize.height
self.ChatTable.frame.size.height += (keyboardSize.height + ViewSend.frame.size.height)
}
}
}
的TableView细胞
import UIKit
class chatlableTableViewCell: UITableViewCell {
@IBOutlet weak var lblchat: UILabel!
override func awakeFromNib() {
super.awakeFromNib()
// Initialization code
}
override func setSelected(_ selected: Bool, animated: Bool) {
super.setSelected(selected, animated: animated)
// Configure the view for the selected state
}
}
你是怎么尝试都没有工作?除了如何获得对象的引用之外,移动“UILabel”确实应该与移动“UITextField”不同。也许显示你正在尝试的代码,并描述什么是“不工作”? – DonMag
您需要在滚动视图中使用该标签并更改偏移以反映键盘的高度。 –