We won't be using Core Data or making Unit Tests for this tutorial, but you are welcome to enable or disable these based on your needs. You can leave the rest of the options as defaults, or fill them out as you like. Make a Cocoa Application, give yourself a Product Name (I use SimpleImageViewer), choose Swift for the Language, make sure check the Use Storyboard option, and uncheck Create Document-Based Application. There is a sample project available on Github for you to see how I did it. The details of how and when you resize the Document View is up to you. Using the width and height constraint constants will ensure that any changes are reflected when the view hierarchy gets laid out. Make outlets for your width and height constraints, and use these exclusively for resizing your Document View by modifying their constant properties, rather than modifying the frame or bounds properties (or overriding the intrinsicContentSize property). Add leading and top constraints from your Document View to the CenteringClipView, as well as width and height constraints within the Document View. For this to work, you need to provide proper constraints for your contained view. Use CenteringClipView.swift as a replacement for the NSScrollView's default NSClipView. SummaryĪ very short summary for the more advanced programmer. Adding things like loading an image file from the filesystem, or adding extended editing functionality, etc are beyond the scope of this document. The image asset will appear centered when it is displayed smaller than the scroll view's viewable area, and it will scroll when it is larger than the viewable area. That asset will be resizable by using Zoom In, Zoom Out, Zoom to Fit, Zoom to Actual Size menu items. The image view will display single asset that will be part of the project. This application will be a simple NSImageView contained as the documentView of a NSScrollView. I will illustrate the salient points, but extending the concept by adding features will be up to you. I am going to keep this project fairly simple for the sake of brevity. A complete, working project can be found at my Github page here. Some familiarity with Xcode, Swift, Storyboards, and basic knowledge of auto layout constraints is required. This tutorial is intermediate difficulty. I am hoping it will be useful to someone, and perhaps relieve them of one less headache. This blog post describes how to construct such a scroll view. There are some older solutions, such as the ones proposed here that are either missing something critical, require old, deprecated code, or simply don't work in modern code.Īfter much experimentation, perusing of various solutions from several sources, and reading much documentation from Apple, I have found a solution that works consistently, and without apparent side effects. It does not take a lot of experimentation before it becomes apparent that naive approaches to creating a centering scroll view, like overriding the ame or NSView.intrinsicContentSize property of the document view, become unwieldy or simply do not work. Fortunately, there are some pointers, and bits of sample code from conferences that we can pull from. Other than that, Apple gives very few hints in their standard documentation. But neither of these options achieve the desired result. A custom view can pin the document view to the upper left by overriding NSView.flipped property getter. The document view of a NSScrollView is pinned to the lower left corner of the window by default. Indeed it is not readily apparent how to do this effectively without incurring strange side effects.Īpple does not give one an easy setup for this behavior. This is such a common idiom, one would think that it would be well supported by default as part of the platform SDK for NSScrollView. View an image that is smaller than the visible window area, and it centers the document view - resize the document view to larger than the viewable area, and the view is scrollable. A notable example would be the Preview Application that comes with OS X. There are many applications available for Mac OS X that center a scrollable document view when the view is smaller than the scrollable area.
0 Comments
Leave a Reply. |