UITextView size not changing size after text has wrapped until second character is typed - ios

I have spent at least 10 hours trying to figure this out and I am stuck. I am trying to have UITextView within a UITableViewCell that automatically expands to the size of the textview when the number of lines in the text view has changed. The table cell is inside a static table view. I have looked at almost every question about this on stackoverflow and nothing seems to be working.
My test project is here.
I have created a custom class that automatically resizes the textview. It works perfectly when not in a table cell. You can see this in the example project. There are two instances of the textview; one in a regular view where the textview resizes correctly, and another in a tableview. In the table cell, it doesn't resize until the second character on the next line has been typed. It is odd, because when I log the height of the textview it says that the value of frame.size.height has changed but the text view does not change size.
One thing that worked was creating a custom table cell and then overriding the layoutSubviews method but the problem with that was the bottom border line on the table cell lagged and created an ugly line across the screen. I looked for a solution but could not figure out how to prevent this.
Thank you for taking the time to read this.

I have tried your project and make it work by :
In Tbl View Controller in storyboard, remove AutoLayout. Although you don't set any constraint for the text view, at run time some constraints will be set automatically, and that will clash when the text view tries to change its frame.
In storyboard, set the autoresizing mask of the text view to default (Fix top, Fix Left, flexible right margin, flexible bottom margin).
It will work correctly now.

Related

How to set Table view cell to occupy entire screen?

I am working on a project to get custom news feeds from Bing news. I created a custom table view cell and populated various UI items in it. But I am having a hard time to make it occupy the entire screen, it starts from extreme left and ends on the right, leaving large gap from the right margin as show in the image. Can anyone help me out, how to correctly fix it with a detailed explanation of how constraints work in Table views and Tableview cells.
Here' what it looks like in the simulator
Remove previous constraints and add constraint to your UITableView like as shown in below image.
So you UITableView will have 0 margin from edges.
Create a UITableViewController. Just drag drop a TableViewCell into it. Add identifier for that cell and use it. By default it will occupy the whole screen.
In your case I guess you might have added constraints to UITableView. Here what you should do is pin your UITableView to Top,Bottom,Right & Left or make UITableView's width equal to SuperView.

Any way to dynamically adjust the height of a UITextView inside a UIStackView inside a UITableViewCell?

My situation: I have a UITextView inside of a UIStackView in a UITableView prototype cell.
After some research and coding, I've implemented the proper code for dynamic table row height adjustment by returning UITableViewAutomaticDimension in the heightForRowAt and estimatedHeightForRowAt delegate methods.
This works, but the problem is that this "dynamic" adjustment isn't dynamic enough. It only adjusts the height of the row to match the height of the UITextView after the UITextView's content is done being edited.
So my goal: to have this same adjustment occur whenever the text view expands dynamically to make all text in it visible, which it already does, yet the table cell doesn't expand with it while editing is still going on (at least I think that's what's happening, but I can't see the second line in the text view because the cell doesn't expand until editing is done).
The intuitive thing to do seems to be to use the textViewDidChange delegate method and to somehow equalize the row's height with the text view's height when text entry or removal results in the text view's height changing...
Yet I can't figure out what to put in there. I tried adjusting the .rowHeight and .estimatedRowHeight for my table view, but that doesn't seem to do anything while editing, neither with static height values nor with UITableViewAutomaticDimension.
Caveat: unlike most other questions about similar issues here on StackOverflow, in my case, the UITextView is inside a UIStackView, which means the stack view controls the size of the text view, and the table row in turn controls the size of the stack view via constraints, meanwhile, I've already gotten the text view to automatically resize to fit its contents, but the table row isn't resizing to match the text view's height while editing is still happening (at least that's the assumption, since I can't visually tell if the text view is expanding while editing is being done, or if it's just wrapping the text to a new line without actually adjusting its frame until editing is complete).
In short: table rows are only resizing to fit the text view/stack view inside them after text view editing is done because neither the table view's delegate methods nor manually setting .rowHeight and .estimatedRowHeight have any effect while a text view is being edited.
Any advice on how to get around this and get the table row and/or text view to resize while editing is happening would be much appreciated.
Turns out that just writing out that question in detail and then looking at a different question on StackOverflow allowed me to find one of several answers that described the solution to this predicament.
The answer:
func textViewDidChange(_ textView: UITextView) {
self.listTableView.beginUpdates()
self.listTableView.endUpdates()
}
Calling .beginUpdates() and .endUpdates() apparently calls the table view's delegate methods without breaking the editing process like .reloadData() does, allowing for truly dynamic table row height changes while editing a text view inside the table row.

Unwanted UIView Created Automatically with UITableView Cell

I am working on an app in the storyboard and building the basic layout. I had a dynamic prototype cell in a UITableView which I created a view in because I thought it would be a good idea to constrain some items in the cell with a view. When I tried to delete that view, I could not. I could not even move any other views outside of it.
I deleted the entire cell and tried to make a new one but even the new one had the view in it, name name too. Everything I tried to put in the cell was automatically put in the view as well.
Now the view is causing me problems because it's height is static and everything is constrained by it. It also seems to take on the roll as superview since everything is inside of it. So any time I constrain anything to the super view, such as the leading edge to superview constraint, it will actually be constrained to the leading edge of that view.
Why is the view being created whenever I make a cell? I have a feeling that it is some setting I mistakenly set but I looked through all of the settings of the view and and cell.
How do I get rid of this view that seems to be tied to every cell?

Resize UITable Header View Auto Layout

I am building a simple iOS application using Swift.
One of my views, displays a list of comments (cells in a PFQueryTableViewController...I am using Parse.com for my database).
My cells resize happily in order to fit the possible multiple-lines of the comments. This is MAGIC! (if you ask me)
What I am having trouble doing, is making the UIView which is at the top of the TableView resize based on multiple lines worth of content for the Label in that top view (see below).
Instead of doing multiline and resizing the view (I have set up constraints and set lines of text to 0), the label only shows one line, truncating it at the edge of the screen with "..."
Does anyone have any suggestions or solution for how to cause my top UIView to resize based on the content of the UILabel?
I was also thinking this could maybe better be done using a Container View at the top of the TableViewController?
There is a delegate method in UITableViewDelegate that lets you return the height of the head view (heightForHeaderInSection). You have to implement it making sure you return the proper height.
If you have a nib with constraints already, you can load the nib in a #property, and use the method systemLayoutSizeFittingSize that should give you enough height.
For more details checkout this post I wrote a while ago: Customize the UITableview with different height of cells like Facebook feeds in iOS

Is it possible set width of UITableView or cells on iPad

Since it's not possible to "hook onto" this and get an answer, and none of the answers do it, I thought I'd ask if anyone ever succeeded in doing it and if so, how.
I've created a New File, selected subclass UITableViewController with XIB, and everything works fine. I can set the height of everything (with delegate methods), but not the frame of anything.
Right now the table is as wide as the iPad and it looks kinda corny with the short titles in each cell - and there is nothing in IB that changes the stretching or layout.
I'd be happy with any solution that causes the cells to shrink horizontally - like margins. I tried scrollview insets in IB, but they only add to the size of the scrollview so it gets bigger than the screen.
I think that you want to subclass UViewController and add a UITableView to it, and make it the size you want in IB.

Resources