Relative Xcode Storyboard constraints - xcode

I have an image with some elements that that I would like to put some text labels on (they are not part of the image because they will be translated). As the image will be scaled and centered in the view these labels cannot have fixed distances from e.g. left and top of the image.
How do I set up constraints in the Storyboard where the x,y position are scaled relatively to the scaling of the image?
PS: For now I've done it programmatically - but I suppose it should be possible to do in the Storyboard.

You’ve to set constraints of labels with imageView
Center label horizontally with imageView
Center label vertically with imageView

Related

Forcing Margins to UIImageView in UICollectionView Cell

I'm looking for a way to force a UIImageView inside a collection view cell to maintain 8 points margins, similar to this picture:
It doesn't work all the time, sometimes it crosses the right edge like this:
The image view is set to AspectFill because I want it to fill the space without distorting it. Here is how it looks in Xcode:
I want to force 8 points margin in all sides except for the bottom.

Xcode Image Slicing Not Working with Gradients?

I have PNG images with rounded corners, inner shadows and a fill gradient, that I thought I could slice in Asset Catalogs horizontal and vertically. But no matter what I try, the gradient slice doesn't stretch or tile as I expected. With Xcode 9.1.
The gradient is not copied (although the inner shadow is), and in IB and the simulator, there are just two bars and no gradient.
What am I missing?
Here are the original image:
Here's how I sliced them:
And what they looked like in IB:
And here's what it looks like in IB (with the colors reversed in the simulator (with a red view and another similar image as background without any gradient) and device.
The center is the part that is getting tiled. You defined a 1x1 square that’s getting repeated on the interior of your image in order to make the image fill the space you defined, which isn’t your stated goal. What you can do is change your center to Stretches, and increase the center’s height and width to take up everything in your image that doesn’t include your rounded corners end caps.

How to keep sprite border the same size when scaling sprite in Unity?

I've created an image in Photoshop to be used as a sprite in Unity and everything works fine while the sprite is scaled at X: 1; Y: 1.
The problem starts when I scale the image up as the border of the image stretches out with the rest of the image. Is there any way to scale an image from its centre or to ignore the image's border when it's scaled?
Here's the example now that I am able to show it:
The rectangle on top is the original image without being scaled up or down and the rectangle on the bottom is scaled at X:5, Y:0.5 but the borders are stretched.
I think that the borders are stretched because it's part of the image and when it's being scaled, the image (including the borders) is just being stretched.
Is there any way to stretch the sprite image but by ignoring the borders?
Are you trying to scale the image and keep the original ratio?
If so, here are the steps:
Hope this helps. Please let me know if you were trying to do something else.
You can use a sliced sprite. The center of the image is scaled to fit the control rectangle but the borders maintain their sizes regardless of the scaling. Check out the Unity doc here: Unity - Manual: Image

Vertical scrolling UIScrollview with Imageview and textview

I spent last 2 days trying to figure out UIScrollview using storyboard. Whenever I think I got it, a new problem appears and now it seams unsolvable.
Here is what I'm trying to do (it's really simple):
I have an Imageview 400x185 taking the whole width of the scrollview (edge to edge). That means that the scrollview has the width of the imageview.
Below is a textview that has <= width of the imageview.
I used many methods described on stackoverflow:
- using a view on the scrollview and setting the width same as the parent of the scrollview.
- using just the scrollview without a view
All of them produced errors in the frame size of the image, size of the subview of scrollview, constrains, textview doesn't show up or the picture is too large even though I set the constrains edge to edge.
Can someone make a sample project with the UI described above? It would take 5 minutes for someone who knows how to deal with it.
I just did this. I know it works. Here is what I would suggest:
Make sure your 400x600 view is constrained to width 400 and height 600.
Add a scroll view to this view. Have it take up the full view and then pin top, bottom, left, and right.
Add a UIView to the scroll view. Have it take up the full view and then pin top, bottom, left, and right. This is your content view. I renamed my in the document outline to "ContentView". At this point, you will have a warning about ambiguous content size, because until you add more constraints, the content view can grow to any size.
The size of the content view will be determined by the contents, so they need to be fully constrained. Add the imageView to the content view. Constrain its width to 400, constrain its height to 185. Pin it to the left, top, and right of the contentView. At this point, the contentView will know it is 400 wide, but it still doesn't know how tall it is.
Add the textView to the contentView. Pin it a constant distance from the imageView. Constrain it to be centered in its contentView. Pin it a constant distance from the bottom of the contentView. Constrain its width to something less than 400 and its height to something big like 600.
At this point, the size of your contentView should be fully constrained. It gets it width from the width of the imageView which is 400 wide and pinned to both sides of the contentView. It gets its height from the constant distance of the imageView from the top + the height of the imageView + the constant distance between the imageView and the textView + the height of the textView + the constant distance from the textView to the bottom of the contentView.
If you want your scrollView to only scroll vertically, then constrain the contentView width to the scrollView width. This is easily done in the Document Outline view by control dragging from the contentView to the scrollView and then selecting Equal Widths from the pop up.

UIScrollView with multipe TextViews and ImageViews layout issue

I have a view in which I placed a scrollView that fits the entire view. Inside the scrollView I have a textView, below a imageView, below a textView and so on. The textViews are filled with content from the ViewController with localized text, so its height will change depending on language. I gave the textViews the constraints for top, bottom, left and right with 10 each. The ImageViews i gave the constraints top and bottom with 10 each, width 200 and centered in container horizontal. The height of the images is different for each image and no constraint for the height is given.
The first result was, the scrollView got a width of the longest text of the localized text. I changed for the
scrollView
Content Hugging Priority Horizontal to 995 and Vertical to 250.
textViews
Content Hugging Priority Horizontal of 400 and 200 Vertical,
Content Compression Resistance Priority Horizontal is 200 and Vertical 750.
Perfect result in portrait mode. When changing to landscape the width of the scrollView stays at 320 and is aligned to the left.
How can I fix the problem to let the scrollView take 100% width of the screen without giving the textViews the chance to force to enlarge themselves to 100% width?
Found solution:
The scrollView has constraints: top/bottom 8 and left/right 0 each to superview.
The textViews have constraints to top, left, bottom and right.
The images have constraints top, bottom and width, centered to X in container.
This results that the textViews take one line and grow to giant width.
Then I centered the textViews to X in Container and selected all textViews and images and centered them too.
Now the scrollView takes 100% width of screen, height as needed no matter if portrait or landscape.
So for all who have maybe the same issue: scrollView looks a bit smaller than the view, its constraints when adding are 0 to left and right and a top/bottom >0. When drag and drop scrollView in the view it takes 100% of the view size. That was the problem, the constraints were -16. Setting the scrollView to x = 16 and width = 288 results in fitting properly to the view! Next giving the textViews constraints top/left/bottom/right and center it to container (even if offset to real center, right to the item is 0 predefined - change it to use current canvas value).

Resources