How to make a triangle on top of the UIView - ios

How can a make a triangle on top of the UIView as a anchor to a UIButton, like in the picture below? I want the triangle to change the position when the other Button is selected. Please only answers written in swift. :)

Your screen shot displays a popover. Use a UIPopoverController and you'll get the triangle automatically.

Sometimes solutions are simpler then expected. Just draw all three triangles in storyboard. Then in your UIController you use the hidden flag to show or hide the triangle.

Related

In iOS, is there a way to add (overlay) a subview of a UIBarButtonItem image without it moving the buttons already in the bar?

I have my swift 5 app working and I'm now adding a 'tool tips' feature, explaining what each part of the screen does.
The approach I have taken is from an article online - add a subview of grey to dim the background, then to that, add a subview of the item being described again, so it is now highlighted, then also, add a subview of an explainer bubble to explain the item highlighted.
This works fine, so long as the UIView I'm using isn't from a UIBarButtonItem. When it is, the bar buttons underneath the grey screen move around to accomodate what they believe is another bar button being added, which causes everything to miss-align. Other buttons do not have this problem, only UIBarButtons.
Any advice is greatly appreciated. Thanks.
Are you adding the duplicate subview to the bar itself? It'd probably be better to add it to the screen rather than the bar so it doesn't affect the bar's layout. In order to get its frame relative to the view controller so you can display your duplicate in the correct position, you could use:
barButtonItem.convert(barButtonItem.bounds, to: self.view)
Assuming self is a UIViewController.

Multi-colored Shadow Swift

In the iOS music app, I've noticed the shadow underneath album artwork changes based on the color of the artwork. This shadow can also be multi-colored based on how the color at the edge of the artwork changes.
Does anyone know how to recreate this effect programmatically?
Thanks to #Josh Homann for pointing me in the right direction, but the answer was actually much more complex.
The first (bottom) UIView in the hierarchy should have a plain white background and be pinned to the edges of the screen.
The next view should be a UIImageView with an alpha of 0.75.
Then you should add a UIVisualEffectView with the same dimensions as the first view.
Finally, add another UIImageView that is the same size as the first UIImageView.
Your IB hierarchy should look similar to this:
The final result is this:
Its not a shadow its a UIVisualEffectView with a blur. Add a second image view under your image view with a larger size and add a blurring visualEffectView to it.
Sorry to revive an old question, but this library nailed it: ShadowImageView.
It uses CIFilter which gives you more control over the blur effect and better results.

Xcode colored square with text next to it. Legend like

I'm trying to create a legend that corresponds to colors on a map. No interaction. What I need is a colored square with text next to it. What I'm struggling with is how to go about creating this. I thought about programmatically creating each square but then I couldn't figure out how to set the text next to it. I then thought about a stack view with a view filled with a view colored and a label. Has anyone done something similar and if so how did you go about doing it?
You can create a square view with right background color, and than position your label next to it.

CGAffineTrasformRotation also rotates the coordinating system

I am rotating a UIView and then adding a new UIView to it. But it looks really weird.
Here is how it looks. The red box is the thing thats not as I expected.
I want the red box to be in the same direction as the blue box.
Im rotating the UIView like this:
[self setTransform:CGAffineTransformRotate(self.transform, DEGREES_TO_RADIANS(90))];
And then adding the red box as a UIView the normal way.
Im thinking that this is just the way transformations work and Im wondering if there is a way to fix it?
That's expected behavior if you're adding the red box after you perform the rotate. If you want the red box to have the same transformation as the blue one, then you'll need add the red box as its subview before applying the rotation transformation.

How to draw a rectangle from UIViewCotroller in iPhone sdk

I am having a UIView and a UIViewContorller to that. I want to draw a rectangle in the view through my controller. So how to do that.
You can add a subview to your view controller's view in viewDidLoad and set the background color to whatever color you want, provided that all you need is a solid color rectangle.
Two options, if you just need a static rectangle you could use an imasge and use UIImageView to display it where you need. If you need to draw it go to this faq
https://developer.apple.com/iphone/library/codinghowtos/GraphicsAndAnimation/#2D-DRAW_LINES__CIRCLES__POLYGONS__AND_BEZIER_PATHS and then to the programming guide to drawing on the iphone, theres some helpful projects they provide to help you out as well.

Resources