UIScrollView always snaps back - uiscrollview

I put a bunch of objects into a UIScrollView and I no have a problem: When I try to "scroll" to some off-screen objects, they appear so long as I am holding my finger down after dragging it, but when I let you, the UIScrollView snaps back to the original position.
Is there a way to prevent this? No off-screen gray area is appaering when this happens, BTW.
Thanks.

You have to set the scroll view's contentSize to include your objects, so it knows how large the scrollable area is — how far to scroll freely before it hits the edge and bounces back.

Try adding a Content Size Fitter component to the Scroll View's Content object and set its scroll direction (Horizontal Fit or Vertical Fit) to Preferred Size.

Related

Using autolayout, how can I center a subview in an NSSplitView without forcing the width NSSplitView to be static?

I have an NSSplitView with content in both NSViews. The left NSView has 2 constraints – Equal Widths and Equal Heights. The right NSView has something simple, say an NSTextField, which is centered via constraints Center X Alignment and Center Y Alighment. This is what I hoped it would look like as I resize the window and/or the NSSplitView divider:
This is what's happening:
I've tried a great deal of configuration changes, I've tried using an NSSplitViewController vs just dropping an NSSplitView into an NSViewController to adjust more parameters programmatically, but I'm not having any luck. Whenever resizing the window, the left view always takes over the excess space. The same happens with the divider (it can be resized, but letting go of the mouse button causes it to snap right back). It seems there's something fundamental that I'm missing here.
The text field's content hugging priority is probably higher than the split view item's holding priority. Fix that and the view should probably work the way you expect.
Also, if, when you resize the view, the left view is resizing with the window while the right view stays the same size, then that suggests that the left view's holding priority may be higher than the right's. You should make the side that you want to stay the same size have the higher holding priority.
That said, I'm not sure what you mean about the constraints you've set on the subviews. "The left NSView has 2 constraints – Equal Widths and Equal Heights." What do you mean here? Its width is equal to what? Its height is equal to what? Do you mean it has an aspect ratio constraint? Frankly, I can't think of what constraints of those kinds would make sense for a view within a split view.

Scaling Fixed Content in Cocoa WebView with Mouse Events

I have a OSX Cocoa WebView that is a subview of view in a NSViewController. The view of the NSViewController has been added as a subview to the main window controller's view.
I am attempting to load HTML5 games into the WebView and proportionally scale them so that they fit the WebView with the least empty space possible.
I have a LOT of games I am trying to make this work for with different constraints.
For HTML5 games that were designed to auto-scale to the window, this works perfectly out of the box.
For HTML5 games that were design a fixed size for iPhone, they appear in the upper left of the WebView and take up a very small portion of the window.
For example, many games are 480x360. The window size is 1024x768 (less the title bar).
I've tried various methods for scaling up the content:
How to resize WebView according to its content?
Scale WebView in Cocoa
I've also tried using javascript to scale the content.
These definitely DO work in scaling up the content and I can get good results in filling the screen.
HOWEVER, this does NOT properly scale the position of the mouse events on the WebView canvas. Meaning, no matter how big I scale the WebView clipview which resizes content, the actual area that is clickable with the mouse is still the smaller unscaled area (as if visually things are scaling but functionally things are not).
Is there some way to scale the mouse click event for position? I can see if I click in the lower right area of the smaller bounding box, it has an affect on the larger scaled content as if I clicked the lower right area.
It seems I could add some scaling math somewhere in capturing the mouse click events but I don't understand where this can be done.
Appreciate any help.
Thanks

Best way to obtain a scrollable OpenGL view with Cocoa

Say I'm writing a 2D cad program of some sort, and I want to be able to zoom in and scroll around my document. However, I also want full control over how my document is drawn and I want an OpenGL context for which to do the drawing. How do I do this? Should I subclass NSScrollView and do something I can't quite figure out there? Should I subclass NSOpenGLView and add a pair of NSScrollers and figure out how to draw them properly? Making NSScrollers and drawing them in a way that looks good natively looks nontrivial, but NSScrollViews seem to want to own all the content you might be scrolling, rather than letting me control the size of the knob of the scroll bar and other such things. I'd be completely content with giving a document size in pixels or some such, just the most important thing to me is that when I draw to (0,0) in my OpenGL context, I draw to the corner of the window, and not into some buffer that NSScrollView owns.
Should I subclass NSOpenGLView and add a pair of NSScrollers
Yes, since scrolling a OpenGL view doesn't make sense. You want to adjust the viewing volume (i.e. the parameters defining the projection matrix), rather than moving your viewport around. And that only works if you have manual control over the scroll bars.

Using Auto Layout to position an NSWindow

Is it possible to use Auto Layout to position a window on the screen?
I'd like to set up constraints to position a window relative to an NSStatusItem. I'd like the window to be centered below the NSStatusItem, but also not be partially offscreen. So, I'd need weak constraints for centering relative to the NSStatusItem and strong constraints for maintaining a minimum distance from the screen edges.
How can I accomplish this?
Auto Layout doesn't work to position windows. The layout engine works per-window to lay out views within that window.
Luckily, this particular layout problem doesn't look too difficult to accomplish the old way. Figure the frame of the window below the NSStatusItem assuming it fits. Then check to see if that frame intersects with the screen edge. If it does, nudge it to the left until it doesn't.

Frames and Bounds (resizing) - Cocoa

I'm having resizing issues, and I think it's because I don't really know enough about frames and bounds.
I have a custom view within a scroll view, which fills the window. When I resize the window, I want the custom view to stay where it is, slowly getting covered/uncovered by the window in the place that the mouse is dragging.
What really happens is the custom view stays anchored to the lower left corner of the scroll view, so that if I make the window shorter, the custom view slides up to keep its lower left corner touching the scroll views corner.
How do I resize the window without moving a particular view?
The frame is the area that the view will occupy within its parent. The bounds is the section of the view that will be drawn within its frame. So 99.99% of the time that the two differ at all, they have the same size but the bounds has a zero origin and the frame has a non-zero origin.
That said, it sounds more like you're confused about the coordinate system. OS X follows the graph paper convention of the origin being in the lower left hand edge of the screen. So your scroll view's origin is in the lower left of the window, which results in that point being the anchor when you resize. The size of the scroll view's frame and bounds changes but the origin doesn't.
Assuming you want the top left to be anchored rather than the bottom left (?), possibly the easiest thing to do would be to subclass NSScrollView and override - setFrame: to do appropriate arithmetic — grab the current documentVisibleRect, work out what's in the top left, allow super to set the new frame then call scrollToPoint appropriately.
If you want to pin your document view to the top left, you can override isFlipped in your document view to return YES. In that case the y coordinate will be flipped and you may need to perform some computation adjustments.
- (BOOL)isFlipped
{
return YES;
}

Resources