我在UITableView
内有UIViewController
。当我开始在UITableView
中搜索时,键盘下会显示一些行
我应该怎么做才能看到所有这些?需要设置任何layoutIfNeeded()或什么?
提前致谢,祝你有个美好的一天!
答案 0 :(得分:2)
如果您可以直接使用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-C代码移植Brandon King:
- (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;
}];
}
答案 1 :(得分:0)
原作者Steve Wilford - 由我转换为Swift 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
}
}