如何用代码Swift.2调整表的高度

时间:2015-12-30 14:38:59

标签: ios swift

我已经做到这一点,所以文本字段始终位于键盘顶部(就像在消息应用程序中,whatsapp ...)但我的问题是,这也构成了它上面的表,因为一切都在滚动视图。我怎样才能使表格调整大小,使其适合键盘和屏幕顶部之间的空间。

enter image description here

1 个答案:

答案 0 :(得分:1)

  1. 您可以在更改框架时调整UITableView的大小。
  2. 例如:

    tableView.frame = CGRectMake(0,0,100,200);
    

    要设置此功能,您应该检查键盘何时存在。用:

    override func 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);
    }
    
    
    deinit {
         NSNotificationCenter.defaultCenter().removeObserver(self);
    }
    
    func keyboardWillShow(notification: NSNotification) {
        yourTableView.frame = newFrame // use CGRectMake()
    }
    
    1. 为什么你想"调整大小"你的TableView?如果它在scrollView上,它不需要调整大小。只需在scrollView上使用 scrollRectToVisible 来显示textBox。
    2. 在此处阅读有关UIScrollView用法的更多信息:

      http://www.appcoda.com/uiscrollview-introduction/

      或Apple:

      https://developer.apple.com/library/ios/documentation/WindowsViews/Conceptual/UIScrollView_pg/Introduction/Introduction.html#//apple_ref/doc/uid/TP40008179-CH1-SW1