Wrong size from UITableView.SizeThatFits() - ios

I implemented an UIPopoverController which is filled with a grouped UITableView. The number of cells in the table view can change dynamicly as long the popover is invisible. As soon as the popover becomes visible, the number of cells are fixed.
When the popover becomes visible, i resize the popover content to the height of the table view. I use the SizeThatFits() method from the UITableView to get the table size and use this value to set the popover PopoverContentSize height.
This works fine when the app is running. But always the first time the popover is shown after a new start of the app, SizeThatFits() delivers a wrong height (about 60 pixel to high). Has anyone an idea what this problem could be?

I had a somewhat similar issue because I was performing my calculations in ViewDidLoad. I was able to work around the issue by creating a bool flag in the view's code and only performing the calculations in ViewDidAppear if the flag was not set (and, of course setting the flag so that the logic wasn't repeated each time).

On iOS 5 and up, sizeThatFits on a UITableView gives the correct result when called within the viewDidLayoutSubviews UIViewController method.

Related

Set max height of UITableView

on the iPhone 5s it seems as though the last item of a specific list shown using UITableView goes slightly off screen and unable to select. Is their any simple way to prevent the tableview from displaying contents offscreen besides checking overtime data is reloaded and the current height of the view?
EDIT: This is all done programmatically and all other tableviews display correctly on the 5s I have tried fiddling with constraints but could not see a difference.
It sounds like your issue has to do with constraints. I'm not sure whether you're using a storyboard to build the view or not, but probably what you need to do is constrain the table view to the bottom of its superview.

scrollToItemAtIndexPath before the view is shown the first time

I have a UIView where I added a UICollectionView. Now this view is shown on app start up. At the same time I'm scrolling to a certain position in my collection view. Therefore I use scrollToItemAtIndexPath. The problem now is that it scrolls to a wrong position, because the collection view has the wrong size at the beginning: If you start the iPad in the landscape orientation on iOS 7 the collection view always takes 768 as width despite it is in landscape.
How can I scroll to a certain position without the user noticing it? Where in the view hierarchy can I call it?
viewWillAppear: frames have not been set
viewDidAppear: not possible without the user noticing it
layoutSubviews: this is called multiple times and I only need it at startup and when the user presses a button
I also tried to use layoutIfNeeded, but that doesn't help for my edge case (iOS 7, iPad, landscape at startup).
Constraints kick around the time viewDidAppear and layoutSubviews is called for the last time. By that time it's too late.
You can try inheriting your own class from UICollectionView and see if its layoutSubviews has correct constraints and call scrollToIndex from there.
If that fails, you can cheat by setting frame sizes in viewDidLoad and calling scroll to index. ( This one is a tad bit more complicated) –

TableView into ScrollView - iOS

I would like to insert a UITableView into a UIScrollView.
To do this I added a ContainerView inside the UIScrollView and a UITableView linked to this ContainerView.
The result obtained in the storyboard is this :
http://i.stack.imgur.com/12hZz.jpg (Storyboard)
http://i.stack.imgur.com/AGY4L.jpg (Storyboard)
When I launch the emulator the TableView is loaded correctly, but not with the result expected
http://i.stack.imgur.com/ebij9.gif (Animated GIF)
http://i.stack.imgur.com/bEO8B.jpg (Wrong result)
But instead what I wanted to to obtain is this:
http://i.stack.imgur.com/R7Zhb.jpg
Is it possible?
Could you tell me if the way to proceed is correct?
Thanks!!
I have not enough reputation to post images and more than two links.
Sorry for my poor english.
So what you have is close, but you need to set the size of the container view to the content size of the table view once it has loaded its data. Your other views around the content view then need to respond to the change in size so you don't get overlaps.
If you know the height of each row in the table and how many there are then it's easiest to calculate the hight required and set that as the size of the content view. If you don't, or the height can change based on user choices or something like that, then you will need to do something more complicated which observes the content size of the table view and updates things when it changes.

Embedded scrollview in tabbar item using storyboards won't scroll

For my current project I wanted to use storyboards and autolayout instead of coding everything by hand. It has gone well so far, but my design has a section of the app where there is a tab bar and one of those tab needs to show four views. The design is to swipe between the four and so I thought to use a scroll view. After some trial and error I found that embedding a Container View in the tab allowed me to easily set up a scroll view and put a couple of view inside it, carefully aligning them using positioning to put them side by side so that each page is one child view. I'm not sure how that plays with the autolayout, and in fact I have the problem that the scroll view won't scroll past the first page position. I can drag about 1/3 of the second page into view, but it never brings that page entirely in view.
I've checked the content size and offset and all of the view positions and it all seems correct. And when I use Spark Inspector to change the scroll offset to the position of the second view/page, the app shows the right page/view and I can even scroll back to the first page. I'm a bit perplexed as to what is causing it to not scroll properly. I don't have any code to show as this is all done in storyboards, but I am wondering if anyone has any ideas as to what is wrong?
Alternately, does anyone have an idea for how to use autolayout and storyboards to set up swiping four adjacent views in a tab? I suspect there are ways to do it. I can think of ways to do it in code, but I'm trying to avoid doing it that way.
EDIT: I set the scroll view delegate to the view controller around it and checked the values of contentSize on scrollViewWillBeginDragging and scrollViewDidScroll. It is always set to {0,0} even after I force set it on viewDidLoad. So I tried setting it every time scrollViewWillBeginDragging is called, which seems work, but I don't understand why this happens and it doesn't smell right. Is there some autolayout constraint that might account for this? Does something cause contentSize get set to {0,0} during the layout process?
For lack of any other answer, I'll use my unsatisfying solution as an answer in the hopes that helps someone else in the future:
I set the scroll view delegate to the view controller around it and checked the values of contentSize on scrollViewWillBeginDragging and scrollViewDidScroll. It is always set to {0,0} even after I force set it on viewDidLoad. So I tried setting it every time scrollViewWillBeginDragging is called, which seems to work.
I'd love to know why the contentSize is being reset if anyone finds out!

ios Autolayout sizeThatFits Issue With View Load Order... or Something

Intro:
The whole metrics table you see here is a view within a metrics viewcontroller.
There is a nearly identical table below in green (doesn't get displayed properly as you can see).
The red area and green area are actually just container UIViews for the viewcontroller's views.
The problem: I must use autolayout for my project. The size of these tables is dynamic (vertically).
It is my understanding that autolayout should be able to determine height automatically for UIViews (using intrinsicContentSize) as opposed to me manually constraining heights. The actual best height of the table in this example is 192. However, when calling sizeThatFits, it returns 405 on the viewcontroller's view. After hooking up the more button to a test method, I found that by calling sizeThatFits on the subviews of the viewcontroller's view, it properly returns a sum of 192. So, I can at least manually constrain the height by using a method to determine the sizeThatFits on the subviews, right..?
Unfortunately, this sizeThatFits method only seems to be able to work once the view has totally loaded on the screen (which explains why it works when using the "more" button test method).
If I attempt to manually determine the sizeThatFits automatically when the view loads, it returns 0 for all views. There seems to be an issue I'm not familiar with when it comes to the order of views loading or something. At what point does this method actually work?

Resources