Autolayout and concentrically smaller nested UIImageViews - ios

I have a photoshop file with 8 concentric 'rings' (although some aren't rings and are more irregular), with the largest at the 'back' and decreasing in size up to the 8th one being very small in the centre.
The 'rings' are drawn in such a way as that the smaller ones are 'internal' to its 'outer' or next larger ring. Each 'ring' has transparency on its outside, but also on its inside (where the smaller rings would 'sit').
I need to support all iOS devices (Universal App).
The largest image has a default size of 2048x2048 pixels, but every one of the 8 layers has a common 'centre' point around which they need to rotate, and around which they need to be fixed.
So, basically, all 8 have to be layered, one on top of the other, such that their centres are all perfectly aligned.
BUT the size of the artwork is larger than any iOS device, and the auto-layout has to allow for every device size and orientation, with the largest (rear) layer having an 8 point inset from the screen edges.
For those that can't picture this, here is a crude representation, where the dark background is 'transparent' and represents the smaller of the width or height of the iOS device (depending on orientation):
Note: The placement of where each smaller UIImageView is precise. They all share a common centre (the centre of the screen) but each ring sits 'inside' of the larger ring behind it. i.e. the centre of the green, hot pink and baby pink circles are empty / transparent, and no matter what size screen or orientations, they have to nest together perfectly, as they do in the photoshop art assets.
I've spent hours in auto-layout trying to sort this out, and when I've got it working on one device and both orientations, it's not working on any others.
No code to show because I'm trying to do this in IB so I can preview on all devices.
Is this even possible using IB / Auto-Layout or do I have to switch to manually working out the scales by which to resize their UIImageView based on screen width / height at runtime and the relationship between each 'ring'?
Edit:
And unless I'm doing it wrong, embedding each UIImageView into a transparent UIView in order to use the UIView to fake 'insets', this doesn't work because those numbers are hard coded, such that when it's perfect on a 12.9" iPad Pro, on an iPhone SE each 'inset' UIImageView is much more compressed and doesn't sit 'inside' it's next larger ring, but is like a tiny letter O with lots of surrounding blank space, because those 'insets' don't scale. What is 100pts on an iPad is a tiny amount of space, but 100pts on an iPhone SE is a 1/3 of the screen.

You can draw circles using CAShapeLayer and UIBezierPath. Since you are trying to fit this in a square, I'd define container size to be either the width or height of the parent container depending on what's smaller, this will allow for rotation and different screen sizes. As for the center, you can always find it by getting center coordinates of your square container (container.bounds.size.width / 2). To rotate your layers/sublayers you can use this answer: https://stackoverflow.com/a/3929703/4577610

Related

Can't resize SpriteKit game properly

So here's my problem. I am currently trying to create a rectangular enclosure using four more rectangles as borders. So it would look somewhat like this. Keep in mind each side is individual so a rectangle with a border wouldn't work.
Now the actual problem comes with different screen sizes. Specifically the 4s. Since the screen is shorter than other phones, how would I resize the rectangles on the side to fit the height? One of my options was detect the screen size, and then set the height of the rectangle, but I was wondering if there was an easier way since I'd also have to deal with all the other screens.
Rather than hardcoding, you can get the maximum screen size with CGRectGetMaxX(self.frame), similarly there are CGRectGetMidX and CGRectGetMinX. With these three methods, you can set proportional height/width and coordinates for the rectangles.

Framing an image in iOS

I'm working on an app using XCode 6 and I'm trying to frame an image. I have one image that will change dynamically depending on the selection in the prior screen. I have another image behind it that is literally an image of a frame. The idea is to have the actual image look like it is surrounded by the frame itself. Here's the trick. I want the actual image to be a certain width to fill most of the horizontal size of the screen (say 85% if you want a number). I want the image to resize to this width based on the width of the screen (based on iPhone 4 vs iPhone 6, for example; so the width sets based on the screen, and the height sets based on the width to maintain its original aspect ratio). The frame should be about 10 pixels wider and 10 pixels taller, leaving 5 pixels around each edge, and the pictures should be centered at the same point.
I've seen a few programmatic fixes for resizing things based on the original aspect ratio of the image. However, I've been primarily using just Storyboard and was hoping to get an answer along those lines. Thanks so much!
Use auto layout and add constraints to the left and right of the frame to the superview. Then add constraints to the image inside the frame to be 10 pixels on top, bottom, left, and right to the edges of the frame. The left and right constraints of the frame should leave everything centered on the screen. Do you have any experience with Auto Layout? It can be a bit of a difficult learning curve, but for your purposes its a perfect solution.

How to work with Ipad images? Xcode

I am creating an Ipad app and no matter what height I set my ImageView to be it is always too big.
For example I have an ImageView that's about 20 pixels tall and has all of the constraints that I need to keep it where I want it to be. When I play this 20 pixel tall ImageView on the Ipad simulator the ImageView covers about half of the Ipad screen.
How can I work with Ipad images if even a 1x1 pixel image view takes up a signifiant amount of the screen? (Yes, I made the 1x1 image just to see if it would still be insanely large)
Under Auto Layout, if you don't set the size of the image view absolutely, it will size itself to match the size of the image assigned to it. That is probably what is happening to you.

iOS Image Resizing / Dealing With Blank Space

I have simply dragged UIImageView into storyboard and made it square. I added a pink background to show the effects of the leftover space in the ImageView. In each case I added either a taller image (1st image) and a wider image (2nd image), as well as a text label. Here are my results.
So the obvious question is....how can I get rid of this extra (pink) space and keep the integrity of the photo (that is, to not have to stretch or lose part of the image)? If I wanted to be able to scroll through photos, it would be nice to have them all the same width to the edge so they look neat and orderly (if they were portrait), and if I wanted to have text under each, I'd want the text to be closer to it, rather than have all the blank (pink) space in between if it were landscape. And obviously different size images will give different sizes of blank space.
So I'm thinking what I could do is before displaying the image, get the size of it, then just have a designated distance from either the label or the edge of screen, depending on the orientation of the picture, and then creating/changing the size of the UIImageView with a bit of math and using the image dimensions before inserting the picture into the ImageView. Is this possible? Is there another method I can't quite figure out?
Just look at any decent photo app and they are nice and neatly organized/displayed despite being different sizes, orientations, etc and I'm wondering how to pull this off. I obviously haven't gotten too deep into using images past simply showing them in a pre-determined ImageView.
Thanks for the help/suggestions!
Try this... set your UIImageView to AspectFit (not AspectFill since that will lose some of the image) and using constraints do the following:
centre the UIImageView in the container both horizontally and vertically
set the UILabel to float below the UIImageView by whatever distance you desire ("standard" is usually good)
set the left, right, and top constraints on the UIImageView to be >= whatever distance you desire
set the bottom constraint on the UILabel to be (once again) >= whatever distance you desire
The effect of this should be that the UIImageView will properly resize itself to its intrinsic size and the constraints should properly position it and the label.

Auto adjust alignment to screen size

I am working on a BlackBerry App that has a lot of ImageButtons, LabelFields and MessageBoxes. What appears to be perfect on one screen size, seems a mess on the other. For instance, Vertical Field Managers that are neatly aligned center with LabelFields, are left/right aligned on bigger screens. Images that cover the width of the screen appear too small on larger screens. Is there some mechanism to auto-align and dynamically change images with respect to the screen size. Any ideas and documents that can help in this regard?
Here are some tips for making screens that look good on almost all devices:
Use less images. If you have to use images, use atleast 3-4 for different screen sizes. for example if you need to have an image as the screen header, use images with widths 320px, 480px and 640px. Load image depending on the width of the screen.
Do not use pixel measurements. Use point measurements instead. Most of the devices are similar in terms of physical size, whereas they have huge difference in pixel density. Using this you can have a screen which will look exactly identical on curve (320x240), bold2 (480x360) and bold 4 (640x480). If you notice, they have the same aspect ratio and similar physical size.
Do not hardcode positions. Instead use FIELD_HCENTER and DRAW_HCENTER etc for fields.
Do not use fonts with fixed pixel height. Use fixed point height instead.
If using custom fields, make sure that they can automatically expand according to device and pixel density.

Resources