gpt4 book ai didi

iOS swift : UITableView KeyPad overlay

转载 作者:行者123 更新时间:2023-11-28 13:11:26 25 4
gpt4 key购买 nike

我在 UIViewController 中有一个 UITableView。当我开始在 UITableView 中搜索时,一些行显示在键盘下方。
我应该怎么做才能让所有这些都可见?需要设置任何 layoutIfNeeded() 或其他东西吗?
提前致谢,祝您有美好的一天!

最佳答案

如果您可以直接使用 UITableViewController,那么这将是首选方法,因为它会自动避免键盘输入。

如果这不可能,您将不得不自己实现。这样的事情应该有效:

override func viewDidLoad() {
super.viewDidLoad()

NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillShow:", name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "keyboardWillHide:", name: UIKeyboardWillHideNotification, object: nil)
}

deinit {
NSNotificationCenter.defaultCenter().removeObserver(self)
}

func keyboardWillShow(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).CGRectValue().size
let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber

var contentInsets = UIEdgeInsetsZero
if UIInterfaceOrientationIsPortrait(UIApplication.sharedApplication().statusBarOrientation) {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
} else {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.width, 0.0)
}

UIView.animateWithDuration(rate.doubleValue) {
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
}

tableView.scrollToRowAtIndexPath(self.editingIndexPath, atScrollPosition: UITableViewScrollPosition.Top, animated: true)

}

func keyboardWillHide(notification: NSNotification) {
let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber

UIView.animateWithDuration(rate.doubleValue) {
self.tableView.contentInset = UIEdgeInsetsZero
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero
}
}

Brandon King 由我自己从原始 Objective-C 代码移植而来:

- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification
object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification
object:nil];
}

- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)keyboardWillShow:(NSNotification *)notification
{
CGSize keyboardSize = [notification.userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];

UIEdgeInsets contentInsets;
if (UIInterfaceOrientationIsPortrait([[UIApplication sharedApplication] statusBarOrientation])) {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.height), 0.0);
} else {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardSize.width), 0.0);
}

[UIView animateWithDuration:rate.floatValue animations:^{
self.tableView.contentInset = contentInsets;
self.tableView.scrollIndicatorInsets = contentInsets;
}];
[self.tableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionTop animated:YES];
}

- (void)keyboardWillHide:(NSNotification *)notification
{
NSNumber *rate = notification.userInfo[UIKeyboardAnimationDurationUserInfoKey];
[UIView animateWithDuration:rate.floatValue animations:^{
self.tableView.contentInset = UIEdgeInsetsZero;
self.tableView.scrollIndicatorInsets = UIEdgeInsetsZero;
}];
}

关于iOS swift : UITableView KeyPad overlay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31137758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com