xjoker123
xjoker123

Reputation: 11

how can i adjust the height of a table with code Swift.2

I've made it so a textfield is always on top of the keyboard (like in the messages app, whatsapp...) but my problem is that this poses the table above it up too because everything is in a scrollview. How can i make it so the table resizes so it fits in the space between the keyboard and top of the screen.

enter image description here

Upvotes: 1

Views: 55

Answers (1)

derdida
derdida

Reputation: 14904

  1. You can resize your UITableView when you change the frame.

For example:

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

To setup this, you should check when the keyboard is present. With:

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. Why you want to "resize" your TableView? if its on an scrollView, it dont need to be resized. Just use scrollRectToVisible on your scrollView to show your textBox.

Read here more about usage of UIScrollView:

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

Or by Apple:

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

Upvotes: 1

Related Questions