我在UITableView里面有一个UIViewController。当我开始在UITableView中搜索时,会在键盘下显示一些行。
我该怎么做才能看到所有这些东西?需要设置任何layoutIfNeeded()或其他东西吗?
提前谢谢,祝您今天愉快!
发布于 2015-06-30 12:08:29
如果您可以直接使用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
}
}我自己从最初的Objective代码中通过布兰登·金移植
- (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;
}];
}发布于 2017-02-15 12:41:23
史蒂夫·威尔福德原作-我将其改编为斯威夫特3
var myIndexPath:IndexPath = IndexPath(row: 0, section: 0)
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(_:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
func keyboardWillShow(_ notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue.size
let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
var contentInsets = UIEdgeInsets.zero
if UIInterfaceOrientationIsPortrait(UIApplication.shared.statusBarOrientation) {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.height, 0.0)
} else {
contentInsets = UIEdgeInsetsMake(0.0, 0.0, keyboardSize.width, 0.0)
}
UIView.animate(withDuration: rate.doubleValue) {
self.tableView.contentInset = contentInsets
self.tableView.scrollIndicatorInsets = contentInsets
}
tableView.scrollToRow(at: myIndexPath, at: .middle, animated: true)
}
func keyboardWillHide(_ notification: NSNotification){
let rate = notification.userInfo![UIKeyboardAnimationDurationUserInfoKey] as! NSNumber
UIView.animate(withDuration: rate.doubleValue) {
self.tableView.contentInset = UIEdgeInsets.zero
self.tableView.scrollIndicatorInsets = UIEdgeInsets.zero
}
}https://stackoverflow.com/questions/31137758
复制相似问题