I have TextView inside a TableView FooterView and that TableView is inside CollectionViewCell.
How to have the TextView move when pressed. Now Keyboard collapses with the textView. It does not scrolls the TableView up by default.
You can use IQKeyboardManager. It will handle all keyboard events.
Take the reference of the bottom constraint of your tableView and then change the constraint constant to keyboard height, then the bottom of your tableView as well as the textView will come just above the keyboard.
Related
I have a custom UITableViewCell with a vertical UIStackView. The stack view in-turn has 2 UILabels, one of it is hidden by default. How to change the height of the cell to show both labels in the stack view properly when the second label is unhidden. On a button click when i show the second label, it tries to show both label with the same height overlapping each other, however when i scroll the view up and down, it re-renders it ok.
I tried to call SizeToFit and LayoutIfNeeded with no help. How do i change the height of the row/cell to show both labels in the stackview to look proper?
You can call
tableView.beginUpdates()
tableVIew.endUpdates()
After tap on button, and your cell change height. It solution is compatible with animation.
On the button click, the label 2nd is showed, you should reload the table view, or reload exactly the cell, the height of cell will change and the cell will be re-rendered
Try reloading that row with reloadRows(at indexPaths: [IndexPath], with animation: UITableView.RowAnimation) after you click the button that shows both labels.
I have a tableview with a textview for entering text immediately below it similar to Apple Messages. When the user begins to enter text and the keyboard appears, I want the following behavior similar to IOS Messages.
If the keyboard will not cover anything, the visible part of the tableview remains unchanged.
If the keyboard will cover something, the tableview moves up just enough so that its bottom-most filled cell is just above the keyboard.
Because I'm using autolayout, I currently have a constraint between the tableview and the textview below it. Also, the project has IQKeyboard which manages a lot of other views involving textfields and textviews.
The constraint combined with IQKeyboard accomplishes 2. When the keyboard appears, the keyboard pushes the textview up. The textview pushes the tableview up. So if the tableview is fully populated, you see the last cell of the tableview above the textview above the keyboard as desired.
However, 2. is not working.
if the tableview is not filled, the keyboard pushes up the textview which pushes up the tableview so that you longer see the top of the tableview.
I have tried adjusting the contentOffset property of the tableview when the Keyboard Shows and this sort of works but the tableview initially moves up before coming back down. I think this is because the notification to change the offset property does not fire until after the keyboard has begun to move up.
I also tried adjusting the tableview height to its content but this causes the textview to expand to fill the difference due to constraints.
Content offset approach - problem is that content offset adjusts too late
//register for keyboard notifications and in handler:
if let infoKey = notification.userInfo?[UIKeyboardFrameEndUserInfoKey],
let rawFrame = (infoKey as AnyObject).cgRectValue {
let keyboardFrame = view.convert(rawFrame, from: nil)
self.heightKeyboard = keyboardFrame.size.height
UIView.animate(withDuration: 0.2, animations: {
self.tableView.contentInset = UIEdgeInsetsMake(self.heightKeyboard!, 0, 0, 0);
})
}
Can anyone suggest a way to mimic the behavior of Apple Messages? Thanks in advance for any suggestions.
One approach:
constrain the top of the tableView to the top of the view
constrain the bottom of the tableView to the top of the textField
constrain the bottom of the textField to the bottom of the view
create an #IBOutlet for the textField's bottom constraint
When the keyboard is shown, change the .constant of the textField's bottom constraint to the height of the keyboard view.
This will move the textField up, and because it's top is constrained to the bottom of the tableView, it will also move the tableView's bottom edge up.
Then scroll to the bottom of the tableView.
Layout:
Initial hierarchy, with 20 rows (scrolled to the bottom):
Hierarchy view (tableView background color set to green, so we can see its frame):
View after the keyboard is shown:
Hierarchy after the keyboard is shown:
Little tough to see from static screen caps, but the frame of the green rectangle (the tableView background) is now shorter... the user can still scroll up and down to see all the rows, but the bottom of the tableView is still constrained to the top of the textField.
When you the keyboard is dismissed, set the .constant of the textField's bottom constraint back to Zero.
You can see a full, working example project up on GitHub: https://github.com/DonMag/KBAdjust
In my project,
In the view 'Top' side is for one UIView and below it a tableView. I want to show tableView to top when scrolled and hide the tableView.
func scrollViewDidScroll(_ scrollView: UIScrollView) {
self.topView.isHidden = true }
Please give me solution for how to show tableView to top when tableView scrolled.
Below is the screenshot
[![enter image description here][1]][1]
in this view bottom is tableView and top is UIView.
Below is the storyboard screenshot.
The simplest solution is to set that topView as tableHeaderView.
self.tableView.tableHeaderView = topView
Now when you scroll the tableView topView will goes up with it. Also no need to implement scrollViewDidScroll now.
Select your tableview in your Controller of Storyboard
Select your TableView
Go to Attributes Inspector on the right panel of Utilities
Set Style to Grouped
Whenever I click on a UITextField, my UIView will move up. How can I stop this?
There could be a chance of one of below.
If UIView is inside(contained) by ScrollView, then set the contentSize to have more height CGSize siz=sv.frame.size; siz.height+=300;sv.contentSize=siz; where 300 is keyboard height.
If there is some code to handle keyboard sizing then search for UIKeyboardDidShowNotification and comment the lines with addobserver, removerobserver in them
I am displaying datepicker from bottom. And added as subview of scrollview. When Picker is on screen I can scroll my scrollview thorugh pickerview also. I dont want to scroll that scrollview at that time and I also dont want to disable scroll at the tiome of displaying date picker. I just dont want to interact scrollview thorugh pickerview. I am using ios7
Don't add pickerview in scrollview.When you show pickerview set scrollview height deducting pickerview height and add pickerview in your view as subview instead of scrollview subview.
scrollview.frame = CGRectMake(scrollview.frame.origin.x,scrollview.frame.origin.y,scrollview.frame.size.width,scrollview.frame.size.height-pickerview.frame.size.height)