我在查找键盘出现时如何向上移动UILabel时遇到问题。
目前,当我使用iPhone 7进行模拟时,标签位于距离顶部大约140像素的位置,并且在键盘出现时位于中心位置。
然而,当使用较小的iPhone 5时,我遇到了这个问题。
键盘与UILabel重叠。
我尝试做的是在键盘出现时将UILabel居中。
大多数教程都展示了如何移动UITextField,但我尝试对UILabel应用相同的方面,但失败了。
答案 0 :(得分:1)
当键盘出现时,向上移动控件的一种可能解决方案是向控件添加底部约束并为这些约束定义出口。
@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
}
旋转设备时也可以。
答案 1 :(得分:0)
如果您正在做约束,只需将乘数更改为0.5。如果您正在做框架,请将框架的y位置(yourLabel.frame.origin.y
)设置为self.view.frame.height / 2
。
答案 2 :(得分:0)
试试这个:制作扩展程序并添加通知观察员
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#define N_SHARED 10
#define SIZE 100
int main(int argc, char **argv){
int shID;
int *myPtr;
int i;
int counter = 0;
int array[SIZE];
for(i=0;i<SIZE;i++){
array[i] = lrand48();
}
/* Shared Memory Creation */
shID = shmget(223520, N_SHARED, IPC_CREAT | 0666);
if (shID >= 0) {
/* get memory */
myPtr = shmat(shID, 0, 0);
if (myPtr==(int *)-1) {
perror("shmat");
} else {
/* memory is accessible */
for (i=0; i<N_SHARED; i++) {
myPtr[i] = array[counter];
counter += 1;
}
// create Child Process
pid_t child = fork();
if(child == 0){
sleep(5);
shID = shmget(223520, N_SHARED, 0666);
if (shID >= 0) {
myPtr = shmat(shID, 0, 0);
if (myPtr==(int *)-1) {
perror("shmat");
} else {
for (i=0; i<N_SHARED; i++) {
printf("%d\n", myPtr[i]);
}
shmdt(myPtr);
}
}else {
/* shmget went wrong */
perror("shmget");
}
}
/* give up memory */
shmdt(myPtr);
}
} else {
/* shmget went wrong */
perror("shmget");
}
}
答案 3 :(得分:0)
First 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 Cell
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
}
}