I have a scroll view full of objects loaded from a json file. Every 3 seconds the data is reloaded, and nothing really changes (unless a user added new data). The reloading is very fast, but the scroll view scrolls back to the top. I want it to remain in the point where it was. How can I do this?
I use this code to delete everything for reloading:
[scrollView.subviews makeObjectsPerformSelector:#selector(removeFromSuperview)];
And then I repopulate the scroll view. When reloading, the frame of the scroll view vil return to the initial value, and only the addition of the objects will make it expand.
This is under the assumption that you are setting the UIScrollView's contentSize with each object received from the JSON file:
When that first object gets loaded from the JSON file again, it sets the contentSize really low, therefore scrolling to the top.
You really don't want to remove everything from the UIScrollView before adding all of the objects back though. You should instead cache the objects that are in there and only do it if there is a change. Even when there is a change I would look into maybe just appending the changes rather than removing and re-adding objects.
Related
I have an NSCollectionView in an NSScrollView.
The scroll view scrolls horizontally to move along the line of items.
Inside each each collection item is a vertically scrolling NSOutlineView.
I have NSButton objects for opening and closing collection items - as supplementary views in my collection.
I set up NSTrackingAreas on these buttons to support mouse over effects.
This works correctly, until I scroll, at which point the NSTrackingArea areas are clearly left behind (the mouse over effects happen when the mouse is where the button was, not where it is).
I rebuild my tracking areas in updateTrackingAreas in my button class, and this is called, but not often enough.
I have tried using .inVisibleRect when setting up my tracking areas, rather than explicitly rebuilding them, but that doesn't improve the tracking update.
I have tried calling updateTrackingAreas on the NSCollectionView when scroll occurs, but it is never passed down to the child views as I expected it would.
As a side note, I also have NSTextViews in my collection view items with toolTips that are very flakey too. They are often left hanging. Pointing hand cursors over links are often misaligned.
It feels as though the default updateTrackingArea is over-optimised and is not being called as often as it should.
So, I am about to embark on building my own tracking-area-tracker to register and update my views when they are not updated by default...
...but maybe someone can see something obvious that I am missing? Thank you.
The tracking area setup can be subtly broken when nesting scroll views incorrectly. See the answers regarding nesting scroll views here for details:
NSScrollView inside another NSScrollView
I want to write a UI test for my Xamarin.iOS app to make sure that all tableview elements/cells exist on a screen. I could use app.Query(e => e.All()), but this will only retrieve elements that are currently visible on the screen. Is there a way to retrieve all elements (including the ones hidden from the view) in order to assert that the retrieved elements are as expected, without initiating a scroll action?
Any suggestions are greatly appreciated.
There isn't a way to do it directly, but there are methods to scroll list views down to get more items, but the original ones will then be removed from the visual tree.
Table views use cell recycling, so they only creates enough cells to fill the screen. As you scroll cells that are scrolled off the top are 'recycled' and shown on the bottom with new data. This means that the cells of screen actually don't exist - so there is nothing that UI test can access.
I've done it in the past by getting the items and caching some values, scrolling, getting more items, scrolling etc, building up a list of items as I go. Then once there is no more to scroll, checking the values.
I have an up that has 2 uiscrollviews. 1 for vertical and another for horizontal content.
In my viewwillappear function I fetch the data from an API to populate these 2 views. I also have a bool to determine if the data needs to be re-fetched based on users actions from within the app. Initially this bool is set to true (fetch data) and once the information is fetched successfully I set this to false (do not fetch). If a user performs some action that requires a re-fetch, I set the bool back to true so the next time they load the view I get the new data.
The issue I am having is that the horizontal uiscrollview has a dynamic number of pages and if I need to re-fetch (after the first time the view is loaded) all my constraints fail and I am unable to see the new data. The first time the view loads everything works perfectly, it's only if I need to re-fetch is when the issue occurs.
I have tried calling
view.layoutIfNeeded()
on the superview and on the uiscrollview but that does nothing.
Thanks!
Managed to work it out. The structure of the scroll views were slightly incorrect in that I did not put the contents inside a separate container UIView.
I'm experiencing a weird behavior in Interface Builder. I almost feel like it's a glitch, but I'm hoping that I'm just misunderstanding something, or that someone knows what weird AutoLayout behavior I'm experiencing, or something. I'll try to describe it as best I can...
I've got a ViewController in Interface Builder, with nothing but a UITextView. Nothing unusual about the TextView, I just dragged it in from the sidebar, and it's the first thing I added. But here's the thing: the text is offset by about 50 points. There's nothing in the Size Inspector that would explain it, the text is just spaced down. BUT, if I click and drag it around, the text pops back up to the top. Then I drop it in a new place, and the text is offset again.
Here's where it gets really weird: if I drag a UICollectionView into that same ViewController, it's fine (the default cell is right up in the top left corner), but as soon as I delete the TextView, the default cell gets moved 50 points down in the CollectionView! Now I have a CollectionView with an offset cell, and if I drag a new TextView back on, it's fine, but deleting the CollectionView adds the offset back to the new TextView! The location of the views is irrelevant (either at the time of adding or later), the older view will always be offset.
And, just for an extra dose of weird: whichever view is offset, if I click and drag it to move it around, the other view will be offset until I drop it in its new location. Oh, and deleting the ViewController entirely and starting again does nothing. The behavior remains.
What black magic is this?! This is the 8th ViewController I've added to my Storyboard (12th if you include abstract ViewControllers like TabBarControllers etc), and none of them had this issue. Why would this forced-offset exist, and how do I stop it? Is this some weird permutation of AutoLayout or something? Or does this sound like a genuine glitch, and I should post on a tech support forum?
EDIT: Just to be clear: the offset does appear when the app is run in the Simulator. IB shows that the frame isn't changing, the content is being offset within the frame, but whatever it is, it DOES show up in the final app.
The answer turned out to be turning off "Adjust Scroll View Insets" on the ViewController.
Sort of a weird behavior for the ViewController to present like that, adjusting a single ScrollView without any regard for its position or size, but at least it was an easy fix.
Ok. I've been at this over and over. I've seen blogs and cocoa dev threads.
I've seen Kyle Sluder's proposed solution, but have yet to find a solution that really works.
How can you position subviews of an NSScrollView with auto layout?
Is it just silently broken ?
Nothing seems to work.
Ok, old question, but this particular issue is a personal bugbear of mine so I'll answer it anyway!
The first thing to note is that an NSScrollView contains an NSClipView, which itself has a view outlet called documentView. These are all added for you when you drag a new scroll view into your storyboard or nib file. By default, the document view is an NSView called simply "View". If you're using a custom view, you can just select this and set its type in the inspector on the right to whatever you want. Otherwise, you'll be adding subviews to it.
The big thing that is easy to miss here is that, by default, the document view has its layout set to 'Translates Mask Into Constraints'. This is fine if the content size will never, ever change, and if that's the case you can simply set the frame of the document view to whatever you want and leave it at that. If you want it to automatically resize itself to fit its content however, there's a few things you'll need to do.
First off, that document view needs to have a completely unambiguous size. If you're using a custom view, I'd recommend giving it an intrinsicContentSize. You should also set 'Intrinsic Size' in IB's inspector to 'Placeholder' and give it a suitable value, or you'll get a bunch of autolayout warnings. If your document view gets its size from its content, all of the subviews must be linked in an unbroken chain from top to bottom, and from left to right, such that the content knows exactly how big it ought to be. This is quite an art in itself, so I won't go into it. A simple example where you have only one subview would be to pin its top, bottom, leading and trailing constraints to its parent, but as noted above if you're doing this, you might as well just set the type of the document view.
Now the fun bit. Select your document view and set its layout to 'Automatic'. Next, add top, bottom, leading and trailing constraints to its superview with a suitable value. I'm using zero, but you might want a small border. Finally, select the TRAILING and BOTTOM constraints you just made and set them to '>=' (greater than or equal) and a priority of 500 or less. The priority is very important, as it has to be less than the priority that the clip view uses to determine its own minimum size. Too high and the clip view will be forced to remain larger than its content, making it impossible in turn for the scroll view to be smaller than its content, rendering it useless.
The technical details aren't important. Just remember to set the document view to layout: automatic, pin all edges, and make the trailing and bottom constraints >= and priority 500.
Note that this will cause your content to hug the top-left corner.
Have you tried setting the document view's setTranslatesAutoresizingMaskIntoConstraints to TRUE?
[_scrollView.documentView setTranslatesAutoresizingMaskIntoConstraints:YES];