首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >iOS Swift : UITableView KeyPad覆盖

iOS Swift : UITableView KeyPad覆盖
EN

Stack Overflow用户
提问于 2015-06-30 12:02:51
回答 2查看 604关注 0票数 0

我在UITableView里面有一个UIViewController。当我开始在UITableView中搜索时,会在键盘下显示一些行。

我该怎么做才能看到所有这些东西?需要设置任何layoutIfNeeded()或其他东西吗?

提前谢谢,祝您今天愉快!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-30 12:08:29

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

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

代码语言:javascript
复制
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代码中通过布兰登·金移植

代码语言:javascript
复制
- (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;
    }];
}
票数 2
EN

Stack Overflow用户

发布于 2017-02-15 12:41:23

史蒂夫·威尔福德原作-我将其改编为斯威夫特3

代码语言:javascript
复制
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
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31137758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档