How to draw a rectangle from UIViewCotroller in iPhone sdk - ios

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.

Related

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.

How to make a triangle on top of the UIView

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.

turn off UIPickerView gradient

I created a UIPickerView that I would like to not have that fade on the edges.
First I thought that must be some kind of gradient overlay so I recursively hid all UIImageViews within the UIPickerView and also set all layer.contents to nil. Setting a background color different to white to the picker view reveals that the fade cannot be an overlay, since the labels in the picker view now fade to the background color.
Does anybody have an idea on how to find that layer mask and turn it off?
EDIT
I just checked if there are any CALayers with masks, but there are none.
This is not exactly the answer to my question but at least a solution. I found a custom (horizontal) AKPickerView on GitHub which is available for Obj-C and Swift (1.2 and 2.0). This View has an undocumented property maskDisabled which does exactly what I need.

Create UIView with custom shape

So I was wondering if there's a way to make a UIView with a custom shape.
I'm trying to make a piano keyboard so when the user touches the view a delegate method responds by playing a noise. The picture is a .png with the picture of the key and a transparent background.
Thanks in advance.
You can't make a UIView in the shape of, say, a triangle, but what you can do is make the view transparent, then add your non-transparent content to it.
For example, to make a view with a background of a png (with, I assume, partially transparent areas) you could make a transparent UIView and then add an image view to it. better yet, just use a UIImageView in the place.

Can I draw the background of a UINavigationBar as the background of my custom view?

I have a custom view which I would like to look like a UINavigationBar. Is there a way for me to draw the background the same way a UINavigationBar would?
I don't want to draw an image or a gradient fill that looks like a UINavigationBar - I want to use the same library code (if it is public) that a UINavigationBar does to draw its background.
The drawRect: method of a UINavigationBar is private - you can't use it to draw the background of your own view.
The easiest way round this would be to take a screen shot, trim the image to nav bar size, add to your project, and then either draw the image in your view's drawRect: method, or add a UIImageView as a subview of your view.
Alternatively, would it be possible to use a UIToolbar instead of your custom view?
You could set your view background image and just use an image that has the navigation bar look already filled in.
As far as tapping into whatever the navBar uses to create itself, you are probably out of luck unless you just build a navBar into your app. Otherwise you would end up having to build a custom view at the top of your app, draw everything in manually. At that point, you are probably better off implementing the navBar.

Resources