Setting UITableViewCell height based on UITableViewHeight BEFORE drawing - ios

iOS 13 (but seems to hold true in iOS 10.3, also).
I'm having a very hard time getting the final height of a view in a complex view hierarchy before anything is drawn to the screen. I have a UIPageViewControl with a series of custom UIViewController. One of the subviews in that is a table view, and I want to adjust the cell height based on the table view's height so that I have an integral number of cells showing.
Unfortunately, the final height of the table view is not determined, it seems, until after all hooks have been exhausted. I've tried all of the following: viewDidLoad(), viewWill/DidAppear(), viewDidLayoutSubviews(), layoutSubviews() and didMoveToWindow() (on the table view and the root view). I've tried calling layoutIfNeeded() in each of the above.
In the end, well after didMoveToWindow() (which comes after viewDidAppear, annoyingly), layoutSubviews() gets called a couple more times, and it’s only in those last two calls that the view has its final height. At this point it’s not possible to set the cell height and reload the table view without it visibly changing on-screen.
There seems to be no way to accomplish this.
Am I missing something? I guess I can try UICollectionView instead.

Related

One of my UICollectionViews is not scrolling or responding to touch events inside my UIScrollView container [Puzzling]

I have an unusual and challenging problem. I have researched deeply on StackOverflow and have been unable to find any solutions. Please do not ask about my design style- it must be this way.
I have a UIPageViewController that contains a UIScrollView which contains 4 UICollectionViews. Each of these collection views should be horizontally scrollable but not vertically scrollable. The scroll view is necessary because the screen is not large enough to display all 4 collection views. Upon loading the screen, the 4th collection view is not immediately visible. After scrolling down, the 4th collection view then becomes visible.
The problem I am having is the 4th collection view does not respond to touches. Specifically, it does not respond to taps or attempts to scroll. The other 3 work perfectly fine. What makes this puzzling and odd is that the 4th one is exactly the same as the other 3, the delegate and data source are set properly and user interaction is enabled. The only real difference between the problematic collection view and the others is that it is not immediately visible when the screen loads.
When I attempt to scroll it behaves as if I am trying to change the page, so the UIPageView changes the page. So the CollectionView isn't registering any touches at all. I have a hunch that it either has to do with the GestureRecognizer from the PageView or something to do with how it isn't visible on the screen upon the initial load.
Any thoughts?
Not 100% sure without seeing the initialization of code or storyboard, but the best thing is that maybe there is a view overlapping. You can run your program and click on the 2 overlapping rectangles. This is the Debug View Hierarchy. It shows you your current frame on your phone and you can see the view laid out in hierarchal status.
Solved my own problem thanks to impression7vx's mention of the Debug View Hierarchy. Had no clue this existed but it let me discover the flaw. I am just writing this to help people who stumble upon this in the future.
I had a content view inside the ScrollView that had a problematic constraint. I set it to have "Equal Heights" with the ScrollView but upon loading the screen the height was static. Only half of the scroll view showed and that half would be registered as the height for the UIView. Since it was static the view would not become larger when I'd scroll, and the 4th collection view would not be inside that container view anymore. For some reason, since the collection view wasn't in the container view it wouldn't register touches.
Solved by manually making the height the same value of the scroll view! This allowed the content view to contain all of the collection views which made them behave normally again.
Just take NSLayoutConstraint of height of the UIView inside the UIScrollView
#IBOutlet weak var scrollContentViewHeight: NSLayoutConstraint!
Set height of this scrollContentViewHeight equal to height of dynamic size of the UIScrollView
if scrollableView.contentSize.height = 840.0
then
scrollContentViewHeight.constant = 840.0
set it on viewDidLayoutSubviews
override func viewDidLayoutSubviews() {
scrollableView.contentSize.height = 840.0
scrollContentViewHeight.constant = 840.0
}

Multiline label in UIView vs UITableViewCell

I created a custom UITableViewCell with two labels and a button.
It behaved perfectly as expected, and looked like this:
Later, I realised I needed to use this particular view multiple places, one of which was not in a TableView. Therefore I decided to delete my cell, and create a custom UIView instead - and create an empty custom UITableViewCell only containing this custom view - constrained to 0 in all directions.
With the exact same elements and constraints, the UIView turned out like this when shown in a TableView as a cell:
Everything is identical. NumberOfLines is set to 0, all the constraints are the same.
I also observed something weird, that when I rotated the entire phone to landscape, it turned out like this:
This is exactly how I'd like it to look when in landscape.
If I now turn it back to portrait, suddenly it looks like this:
Now it suddenly looks exactly like I want it to look in portrait.. I just had to flip it to landscape and back to portrait.. Why is that?..
I played around with some variables, among them: preferredMaxLayoutWidth, and found that if I set this value to the size of the screen (minus the 16*2px padding) it looks as expected at first launch in portrait. However, I don't want to set this value. There is no magic number in px that will be right. The whole point of this is to get it fit properly on all screen sizes and orientations. I probably could re-set the preferred width every time superview's layoutSubviews or something is called, but I figured there has to be a solution for this..? Why did it work for UITableViewCell and not for UIView inside a UITableViewCell?
I have tried all sorts of sizeToFit etc., and don't find any questions or answers that targets this particular difference in UIView and UITableViewCell..
The problem here is a bit complex but I will try to explain it... When the view lays out, it initially does so independently of its container and then reports its height to the system when the system needs to determine the height of the cell, which happens before the cell is constructed. Once the cell exists and the device is rotated, the view "realizes" it's in a cell and constraints itself and reports its height appropriately.
Sadly, this is an issue in the table view system stemming from legacy issues with how cells were originally sized back before constraints existed.
The simplest solution is to put the labels and button back directly in the cell. Another option might be to make your external view a table view cell. You can instantiate a table view cell outside of a table view for that one spot it's needed in. Another option is to implement the table view delegate's tableView:heightForRowAtIndexPath: method and manually determine the size.
I am assuming that you are using following method for table view's height:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
So, Just add all your view's in stack and set top, bottom, leading and trailing constraints of UIStackView with respect to UIView of xib as:

Getting real width of UITableViewCell's contentView before it displays

I have a grouped table view with custom cells (created programmatically by subclassing, not with IB). To properly position custom cell's subviews (such as labels and text fields), I need to know the current width of the cell's contentView just before the cell displays (taking into account that real cell width in a table view can change (according to screen orientation, modal presentation style, etc.)).
if I override in custom cell class the layoutSubviews method, it works perfectly, but it can be called frequently, thus I have to reposition my subviews every time when it's called, even when there's no need to do that.
Please, recommend me more elegant solution.
The recommended way of doing this is by setting the autoresizingMask of the table cell. If you need to have more control over the layout, you can store the last used view width in a member variable, and only layout the subviews if this differs from the current view width.

Sticky UITableView, unnecessary horizontal bar is visible, and I can't scroll to the bottom of the table

I've created a UITableVIew and it is a subview of a UIView. There are three issues that i'm having and it is only occurring on iOS 4 devices:
1) The table doesn't bounce when the view hits either the top or bottom of the table while scrolling. The vertical bar doesn't shrink either, it feels sticky and it is very much acting like an Android table view. I've tried enabling the bounce property but that doesn't make any difference.
2) The horizontal scroll bar appears when the view is scrolled down to the bottom of the table. This shouldn't appear since the table view's contentSize has been set correctly. It does eventually disappear when the contentSize is set 20 pixels less than what it should be.
3) I can't scroll to the bottom of the footer view, and only half the footer view is visible.
I've added a UITableView as a subview to other views throughout my project and this has never occurred, and so i've copied the way that I create other UITableViews, but still no luck.
Any suggestions would be greatly appreciated.
Thanks,
Ankur
I worked out the problem. The problem is a little strange, although i had a feeling that the way i was coding was a little messy.
I am subclassing a View, and the parent view has a layoutSubview method, which is only calculating and setting the frame for a table view subview. In the subclass, i had to override layoutSubview for the same reason as the values for the table view's frame need to be different. This means that the table view's frame was being laid out twice, once by the parent class and a second time by the subclass. It seems that iOS 4 doesn't like this, and i should only set the frame once per subview per layoutSubview call.
Now i've created a layoutTableView method, which is called from the parents layoutSubview, and i've overridden layoutTableView in the subclass. Therefore the table view's frame is only being set once.

Resized UITableView's cells' frames are not updated

I have a UITableView that I resize when the keyboard shows.
// MonoTouch code
// Resize the table so it's not hidden by the keyboard
var keyboardFrame = UIKeyboard.BoundsFromNotification(notification);
var tableFrame = myTable.Frame;
tableFrame.Height = theView.Frame.Height - keyboardFrame.Height;
myTable.Frame = tableFrame;
// Bring the current cell back in view
this.InvokeOnMainThread(()=>{
if(currentCell != null) myTable.ScrollRectToVisible(currentCell.Cell.Frame, true);
});
However, when I try to perform an action on that cell that is sensitive to that cell's current position on the screen (such as attaching a UIPopoverController), I find that the frame of that cell is showing the original location prior to the table resize and scrolling.
I've tried the following all yielding the same results of the frame still pointing to the old location (a lot of these were experimenting to see if they did anything not knowing what would work and what wouldn't):
Using SetNeedsLayout on the cell, the subviews of the table, the table.
Using LayoutIfNeeded on the cell, the subviews of the table, the table and the view containing the table
Multiple combinations of SetNeedsLayout and LayoutIfNeeded
Spawning a new thread, sleeping on that thread for a ridiculous amount of time (to allow time for the UI to perform a redraw/update/whatever) and then looking at the Frame
Spawning a new thread, sleeping for that ridiculous amount of time and then calling the different layout functions
I'm not certain if it's the resize or the scrolling that is where the breakdown is happening.
When you scroll the table using a gesture, the cell frames seem to be updated properly because I can always show a popover view in the correct location relative to that cell I'm attaching it to. So I don't understand why resizing the table and scrolling it programmatically wouldn't do the same.
Does anyone know how to force a UITableView to update it's cells' frames without doing a ReloadData (ReloadData causes many unwanted effects in this case)?
This is behavior is expected. The UITableView inherits from UIScrollView. The UIScrollView, apart from its frame, has another property, ContentSize. The two are separate. While the frame represents the position and size in relation to its parent view, the ContentSize defines the area of the content of the scroll view, so that the object will "know" by what amount to scroll each time.
Changing the frame or bounds of the UITableView, or any view for that matter, does not necessarily change the frame(s) of its subviews. This behavior depends on the superview's ContentMode property.
Anyway, the table cell's frames do not change, because they are relevant to the ContentSize property. Since it is working correctly before resizing the table view, I can only assume that the ContentSize and Frame of the table view match at that point. After resizing the table view, the ContentSize remains intact so you have inconsistency between the two values.
Instead, you should use either the ConvertRectToView or ConvertRectFromView methods to get the rectangle of the cell in relation to the tableview's frame.

Resources