Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 通用UITableView键盘大小调整算法_Iphone_Ios_Ipad_Uitableview - Fatal编程技术网

Iphone 通用UITableView键盘大小调整算法

Iphone 通用UITableView键盘大小调整算法,iphone,ios,ipad,uitableview,Iphone,Ios,Ipad,Uitableview,我已经搜索了很多调整表视图大小以适应键盘显示和隐藏的代码,但是我遇到的几乎每一篇文章都假设表视图占据了其视图控制器的整个视图。我有一个iPad应用程序,其中表格视图只占屏幕的一部分。在这种情况下,调整表视图大小的正确方法是什么?(我上面提到的帖子中的所有代码都失败了),它是拖放式的,所以只需将tablview声明为typeTPKeyboardAvoidingTableView简单解决方案-注册以在init或viewDidLoad上接收键盘通知,方法是: [[NSNotificationCente

我已经搜索了很多调整表视图大小以适应键盘显示和隐藏的代码,但是我遇到的几乎每一篇文章都假设表视图占据了其视图控制器的整个视图。我有一个iPad应用程序,其中表格视图只占屏幕的一部分。在这种情况下,调整表视图大小的正确方法是什么?(我上面提到的帖子中的所有代码都失败了)

,它是拖放式的,所以只需将tablview声明为type
TPKeyboardAvoidingTableView

简单解决方案-注册以在init或viewDidLoad上接收键盘通知,方法是:

[[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(keyboardWillShow:)
                                           name:UIKeyboardWillShowNotification
                                         object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
                                       selector:@selector(keyboardWillHide:)
                                           name:UIKeyboardWillHideNotification
                                         object:nil];
然后,当收到通知时,您可以使用键盘的给定矩形来调整tableView的框架:

- (void)keyboardWillShow:(NSNotification *)notification
{
   // Get the size of the keyboard.
   CGSize keyboardSize = [[[notification userInfo] objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;

   CGRect newTableFrame = _myTableView.frame;
   //Here make adjustments to the tableview frame based on the value in keyboard size
   ...

   _myTableView.frame = newTableFrame;
}

- (void)keyboardWillHide:(NSNotification *)notification
{
   //Here change the table frame back to what it originally was.
}

下面的代码执行您想要的操作,适用于任何设备和任何布局。代码由(经允许复制和使用)提供

b。上面使用的IVAR声明如下:

BOOL keyboardShown;
CGFloat keyboardOverlap;
c、 "self.activeCellIndexPath’始终设置为拥有当前活动UITextField/UITextView的单元格的indexPath


享受!:)

我发现最简单的解决方案是这个(我不喜欢使用子视图处理这类内容):

注册键盘帧更改通知(理想情况下,在视图中注册将出现:而在视图中取消注册将消失:):

然后在方法上:

- (void)keyboardDidChangeFrame:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect kbIntersectFrame = [window convertRect:CGRectIntersection(window.frame, kbFrame) toView:self.scrollView];
    kbIntersectFrame = CGRectIntersection(self.bounds, kbIntersectFrame);

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbIntersectFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}
或者,如果您想在更改contentInset后摆脱“跳转”:

- (void)keyboardDidChangeFrame:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect kbIntersectFrame = [window convertRect:CGRectIntersection(window.frame, kbFrame) toView:self.scrollView];
    kbIntersectFrame = CGRectIntersection(self.scrollView.bounds, kbIntersectFrame);

    // get point before contentInset change
    CGPoint pointBefore = self.scrollView.contentOffset;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbIntersectFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // get point after contentInset change
    CGPoint pointAfter = self.scrollView.contentOffset;
    // avoid jump by settings contentOffset
    self.scrollView.contentOffset = pointBefore;
    // and now animate smoothly
    [self.scrollView setContentOffset:pointAfter animated:YES];
}

简单的解决方案是将my extension
UIViewController+Keyboard.swift
添加到您的项目中,只需一行
setupKeyboardNotifcationListenerForScrollView(tableView)
,它就会自动调整大小。不需要子类化任何东西,只是一个扩展!其开源代码位于

以下是键盘方法:

func keyboardControl(notification: NSNotification, isShowing: Bool) {
    var userInfo = notification.userInfo!
    let keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue
    let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.unsignedIntValue

    let convertedFrame = self.view.convertRect(keyboardRect, fromView: nil)
    let heightOffset = self.view.bounds.size.height - convertedFrame.origin.y
    let options = UIViewAnimationOptions(rawValue: UInt(curve) << 16)
    let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

    //your UITableView bottom constrant
    self.tableViewMarginBottomConstraint.constant = heightOffset

    var contentInsets = UIEdgeInsetsZero
    if isShowing {
        contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardRect.size.height), 0.0)
    }

    UIView.animateWithDuration(
        duration,
        delay: 0,
        options: options.union(.LayoutSubviews).union(.BeginFromCurrentState),
        animations: {
            self.listTableView.contentInset = contentInsets
            self.listTableView.scrollIndicatorInsets = contentInsets
            self.listTableView.scrollBottomToLastRow()
            self.view.layoutIfNeeded()
        },
        completion: { bool in
    })
}
func键盘控制(通知:NSNotification,isShowing:Bool){
var userInfo=notification.userInfo!
让keyboardRect=userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue
设curve=userInfo[UIKeyboardAnimationCurveUserInfo]!。无符号值
让convertedFrame=self.view.convertRect(keyboardRect,fromView:nil)
设heightOffset=self.view.bounds.size.height-convertedFrame.origin.y
让选项=UIViewAnimationOptions(原始值:UInt(曲线)Int{
变量i=0
var rowCount=0
而我
你可以通过使用实现你想要的,它是一个无代码库,你只需将它添加到你的pod文件:pod'IQKeyboardManager',就是这样,即使UITextField/UITextView不是scrollView/tableView的一部分,当显示键盘时,它也会产生滚动效果。

你没有提到任何失败的代码。W为什么不调整您找到的代码来解释表视图框底部和完整视图框之间的差异?我建议在视图中注册将出现:而在视图中注销将消失:(由于通知中心观察员保留,并且由于viewDidUnload:(用于注销)自iOS 6以来已被弃用)这真的很好,比上面的Tarek更简单。我不使用UIWindow进行挖掘,但应该很容易切换到视图,不是吗?是的,理论上。请记住键盘坐标是“窗口尺寸”因此,如果视图的窗口大小/位置不相等,则无法正常工作。我只会添加一项。如果UITableViewCell高度可以大于显示UITableView的剩余大小,则最好修改tableAnimationEnded方法,使其滚动到“顶部”,而不是“无”。此外,如果在选择单元格时显示键盘,并且已经处理了UITableView选择,则无需再次选择“activeCellIndexPath”。
- (void)keyboardDidChangeFrame:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect kbIntersectFrame = [window convertRect:CGRectIntersection(window.frame, kbFrame) toView:self.scrollView];
    kbIntersectFrame = CGRectIntersection(self.bounds, kbIntersectFrame);

    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbIntersectFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
}
- (void)keyboardDidChangeFrame:(NSNotification*)aNotification
{
    NSDictionary* info = [aNotification userInfo];
    UIWindow *window = [[[UIApplication sharedApplication] delegate] window];
    CGRect kbFrame = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect kbIntersectFrame = [window convertRect:CGRectIntersection(window.frame, kbFrame) toView:self.scrollView];
    kbIntersectFrame = CGRectIntersection(self.scrollView.bounds, kbIntersectFrame);

    // get point before contentInset change
    CGPoint pointBefore = self.scrollView.contentOffset;
    UIEdgeInsets contentInsets = UIEdgeInsetsMake(0.0, 0.0, kbIntersectFrame.size.height, 0.0);
    self.scrollView.contentInset = contentInsets;
    self.scrollView.scrollIndicatorInsets = contentInsets;
    // get point after contentInset change
    CGPoint pointAfter = self.scrollView.contentOffset;
    // avoid jump by settings contentOffset
    self.scrollView.contentOffset = pointBefore;
    // and now animate smoothly
    [self.scrollView setContentOffset:pointAfter animated:YES];
}
func keyboardControl(notification: NSNotification, isShowing: Bool) {
    var userInfo = notification.userInfo!
    let keyboardRect = userInfo[UIKeyboardFrameEndUserInfoKey]!.CGRectValue
    let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey]!.unsignedIntValue

    let convertedFrame = self.view.convertRect(keyboardRect, fromView: nil)
    let heightOffset = self.view.bounds.size.height - convertedFrame.origin.y
    let options = UIViewAnimationOptions(rawValue: UInt(curve) << 16)
    let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey]!.doubleValue

    //your UITableView bottom constrant
    self.tableViewMarginBottomConstraint.constant = heightOffset

    var contentInsets = UIEdgeInsetsZero
    if isShowing {
        contentInsets = UIEdgeInsetsMake(0.0, 0.0, (keyboardRect.size.height), 0.0)
    }

    UIView.animateWithDuration(
        duration,
        delay: 0,
        options: options.union(.LayoutSubviews).union(.BeginFromCurrentState),
        animations: {
            self.listTableView.contentInset = contentInsets
            self.listTableView.scrollIndicatorInsets = contentInsets
            self.listTableView.scrollBottomToLastRow()
            self.view.layoutIfNeeded()
        },
        completion: { bool in
    })
}
extension UITableView {
    func totalRows() -> Int {
        var i = 0
        var rowCount = 0
        while i < self.numberOfSections {
            rowCount += self.numberOfRowsInSection(i)
            i++
        }
        return rowCount
    }

    var lastIndexPath: NSIndexPath {
        get { return NSIndexPath(forRow: self.totalRows()-1, inSection: 0) }
    }

    func scrollBottomToLastRow() {
        self.scrollToRowAtIndexPath(self.lastIndexPath, atScrollPosition: .Bottom, animated: false)
    }
}