Scale Image to image view - swift

I am currently building an iOS app. I'm still in the very beginner stage but know my way around programming.
However the Interface Builder gives me a headache.
I want to have an image view and then programatically load the image content (no problem here). However I want the image view never to change the initial size (now matter what the resolution of the image is).
Here is the trick though: The image view should be a certain distance from the left, right and top. On the bottom the imageview needs to keep a minimum distance but can be as large as possible (for different iPhones).
I am still lost what mode I need to set for the imageview, because currently every single setting resizes the view somehow.
Anybody got any advice on how to set the mode and the constraints?

You need to implement windowWillResize (or the live variant) from NSWindowDelegate and calculate the scale for the image in the view before re-displaying it.

Related

How to resize an NSImageView in an NSStackView?

I'm working on a project where I want the following in an NSTableViewCell:
Image
Text
Subtitle text
The NSTableView is in a window which the user can expand or contract. When the window expands and contracts, the text wraps as needed.
That much is working.
When it comes to the image view, I can't get the thing to resize at all. I don't understand how the text automatically wraps, but images don't automatically scale. I've been working on iOS so long that I might have missed something in how stack views differ between iOS and macOS, but I never had this problem in iOS.
I don't have much code because the text wraps properly without any code at all, so instead I posted a minimal project showing the problem on Github:
NSImageTableViewTest
Some things I tried:
I have to set the width/height of the image view, or the text won't wrap. It seems to me that the reason why is that if I keep the image view unbound, the table view starts at the width of the image.
I tried setting the leading and bottom constraints of the image view to no avail.
I tried setting the constraints of the NSStackView, but that doesn't help constrain the frame of the image view.
Question: do I have to change the frame of the image view in code? I did try that, to no avail.
At this point, I'm stumped and I'm sure the fix is something easy that I overlooked.
Thanks.
The image view doesn't shrink because the default Content Compression Resistance Priority is too high. Set the Content Compression Resistance Priority to (a bit lower than) "Low (250)".

How to find the largest size of a view that does not make window larger than screen size?

I'm working on a MacOS application that needs to display large images. if I naively set the ImageView to be the full size image, the application's window can be forced to become larger than the screen size. What I'd like to be able to do is work out how large I can make the image while keeping the entire window (which also contains other UI elements) on screen.
I know I can query the amount of available screen space using NSScreen:visibleFrame() but that does not seem to be much help since unless I make a whole load of assumptions about how much space the rest of the UI will take up which then defeats the point of having constraint based UI layout.
The other approach could be to find a way to constrain the window size and then let Cocoa work out the sizes of the views. However, it looks like the UI editors in Xcode only allows static sizes to be specified which is not much help.
It looks like this is possible in SwiftUI (https://www.hackingwithswift.com/quick-start/swiftui/how-to-adjust-the-way-an-image-is-fitted-to-its-space)
Image("example-image")
.resizable()
.aspectRatio(contentMode: .fit)
However, the application I'm working on needs to run on older versions of MacOS than supported by SwiftUI so I need to know how to do this in swift but using plain Cocoa.
The first thing I would try is to modify the priority of the constraints that make the image view large enough to accommodate the image. That would be its content compression resistance priority. I think if you set it to .dragThatCanResizeWindow then you might get the behavior you want for free. That's because the system already constrains a window to the screen size when you drag its edges. So, presumably, that implicit constraint has a higher priority than .dragThatCanResizeWindow.
If that doesn't do it, you can programmatically set a window's maximum frame size by setting its maxSize property. You'll want to set that each time the screen configuration changes or the window moves to a different screen. For the former, you can observe NSApplication.didChangeScreenParametersNotification. For the latter, your window delegate can implement windowDidChangeScreen(_:) or you can observe NSWindow.didChangeScreenNotification.

Xcode constraints and uiimageview as a background

I am sorry if I am going off topic, this is not a question about code, but I am having troubles with constraints
In my app I use a UIImageView as background, I set the constraint for the image to fit the screen and it works
But after that i have a lot of labels and buttons,and if i add even one constraint to one of these elements,it disappears or goes out of the UIImage
I want to set the UIImageView as background,so all the other elements can be placed on the image using constraints ,how do i do it?
To simplify the process can i group all of my interface elements in some views and then apply the constraints to the view?
I need a good explanation and/or tutorial about this elements, at the moment my interface on iPhone 4 fits ok until the iAd shows up, and on the other iPhones there is a lot of empty space
take a look at this tutorial. Hopefully, it will works.
http://www.raywenderlich.com/83276/beginning-adaptive-layout-tutorial
Try this: with one of you elements, cmd+click (or right-click) on it and drag upward until the background UIImage is highlighted. Release and select 'left'. This sets a pinning constraint to the left side of the background image.
Then cmd+click and drag to the side, and select 'top'. This should pin your element to an intelligible spot; it may still need some sizing help, but at least you will be able to guess where it will show up.

Create a UIScrollView that can be embedded into another view

I'm trying to build a custom, reusable UIScrollView that can be added to multiple views. The scroll view is going to be a weight picker. For the life of me, I can't find a decent example for how to implement this neatly or cleanly.
I would love for someone to point me to an existing library or tutorial that shows me how to do this. I've hacked apart a few examples, but so far, nothing is very good or reusable. Please help!!
For what it's worth, I have an image that individual ticks for the weight. So I can select to the tenth of the number (e.g. 160.4). The image has the first tick bold and larger than the remaining 9. I'd like to have the weight/number centered over the large tick. I'll update the points to my label/datasource after scrolling stops.
UPDATE
I need to make this. I have the custom font, background, and ticker image.
I would not do this through an UIScrollView. I think it would be more complex and you would certainly end up having issues when trying to add you custom picker into another scroll view.
What I would do is:
building the picker view by means of a series of CALayers, each one representing a "building block" of your picker view; see attached image:
each building block would represent a specific value by mixing a UILabel (the text) and an image;
use a pan gesture recognizer, or alternatively define touchesBegin/Moved/Ended method to deal with panning;
when a pan is done, you displace the view content to the left or right according to the panning;
when panning, you also add new building blocks to the left or right end of the picker to account for empty areas that would be revealed by the displacement done at point 4.
I think that having a look at another kind of custom control source code would be of great help to you. You would not possibly find your custom picker already implemented, but could get some guidance. Have a look then at cocoa controls.
Hope this helps.
If I were going to implement this, I would create a really wide image that had every weight on it I'd ever need - I would probably create this in code when the app started up. This image is then used as the contentView of your picker. You get all the scrolling features "for free", and you could even update the values shown in the other parts of the view during scrolling (or dragging.
The scrollView is just the area with the tick marks and weight numbers, and resides in a subview above the background, but below the centered vertical line that shows the actual weight.
EDIT: on second thought, forget the image. If you have the code to draw the image, you can do the drawing in a custom UIView. So you get the draw rect, you know the contentOffset, so you can draw just what you need.

iOS playing cards

I currently have an image which has every suit and value of a deck of playing cards in it. There is an UIImageView to show this picture, with width and height set to show just one card at a time and view mode set to "top left" so it doesn't stretch.
Now I'm just showing what happens to be the top left card, the ace of clubs. I tried finding a way to offset the view to show a different portion of the UIImage, but couldn't find a suitable property (tried fiddling with "frame" and "bounds").
Is there a way to get such an offset working, or am I going about this the wrong way? I'd like to keep all the cards in one image in "sprite sheet" style so that they would be easy to modify later instead of having a bunch of separate pics.
Set the bounds to what you'd like to show, enable 'clipToBounds' and make sure the content stretch mode is set properly. That should work.
EDIT: I do recommend making different images though, because of memory issues (even though not the whole image is shown, you still fully have it in memory).