UIImageView subclass for circle icon in IB - ios

I need to create a UIImageView subclass that cuts an image to a circle shape, and draws a border.
I set the image view and image in a storyboard.
The initWithCoder method of my subclass is called, but other methods, such as drawRect: and setImage: are not called (I set breakpoints in every method). And the image that appears is not cropped.
Why are drawRect: and setImage: are not called?

Why you don't use imageView.layer?
for example:
imageView.layer.cornerRadius = imageView.frame.size.height/2.0f; // for circle shape
imageView.layer.borderWidth = 3.0;
imageView.layer.borderColor;
imageView.clipsToBounds = YES;
P.S. I'm sorry that i write here, small reputation...

Related

Make a UIView with a blurred view as borderColor

I need to create a UIView with a blurred view as borderColor.
How can I get that to work?
I have attached a sample image as an example of my requirement.
Thanks
I don't think the border of a UITextField has a fading property, or anything similar. However, with a bit of work, you can accomplish what you need:
1) Download/acquire a background image that has the border you want. It can be rectangular. Add it to your project.
2) In IB, set the BorderStyle of you TextView to None, the BackgroundImage to the image you added, and add an outlet to your UIViewController class.
3) Add the following code to your viewDidLoad. Replace with the name of your IBOutlet:
<textField>.layer.cornerRadius =5.0
This should give you a UITextField with rounded corners and your background image. You may have to play around with your image and the cornerRadius values to get exactly what you want, but the code should work.
Use my code its work perfectly. Textviewview is my One UIView and inside it one textfield and one UIImageview i use.
Textviewview.layer.cornerRadius = 25.0
Textviewview.layer.shadowColor = UIColor(white: 0.0, alpha: 0.5).CGColor
Textviewview.layer.shadowOffset = CGSizeMake(0.0, 0.0)
Textviewview.layer.shadowOpacity = 1.0
Textviewview.layer.shadowRadius = 6.0

Get uiview section under uiimageview?

I have an UIView as background, and a UIImageView above it.
What I want to do is fill the UIImageView with the UIView section that is in the back (without the white border)
I tried cropping a snapshot of the background but it doesnt look good. there is always a difference.
Make the background UIView a subview of the UIImageView and then set the property of the UIImageView yourImageView.clipsToBounds = YES
Use CALayer mask. The mask will be the smaller image view, and will be assigned to the background view's mask property.

UIView border cover sub-view?

I have a UIView which includes a UIButton which is partially on UIView. I have a problem when I draw a border on my UIView. Please have a look at my screenshot:
You can see the border is above the UIButton, why? Can anybody suggest? Thanks
Thanks for aăâ, I found a solution.
Basically the border is always drawn on top of everything
What I did is:
Create a UIView with color of border
Create another UIView as the child the main UIView which is a little bit smaller than the first one. The color of this newly create UIView is the main color
Here is the code:
self.layer.cornerRadius = 15;
self.layer.masksToBounds = YES;
self.backView.layer.cornerRadius = 15;
self.backView.layer.masksToBounds = YES;
The result is:
It's more or less what I need although it's not perfect.
It could have to do with the order that the objects are drawn. In your storyboard's "Document Outline", views that are lower down in a view controller's outline are drawn later. Perhaps the button is not the last drawn view, like you want?

Overlaying UIView with an background image

Is it possible to overlay UIView with an image without using UIImageView from the Interface Builder?
If not, how would you accomplish the same in code?
From the UIView documentation:
Image-based backgrounds - For views that display relatively static content, consider using a UIImageView object with gesture recognizers instead of subclassing and drawing the image yourself. Alternatively, you can also use a generic UIView object and assign your image as the content of the view’s CALayer object.
Using the second option, to set someView to have a background of yourImage.png:
someView.layer.contents = (id)[[UIImage imageNamed:#"yourImage.png"] CGImage];
if by Overlying a UIView you mean painting its background with an image, it can be done like this:
someView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"yourImage.png"]];
You could also add a new UIImageView that covers your UIView
by code, Init your UIImageView with the same frame Rect as your UIView and add it as a subview.

Blacking out areas of a UIImageView or showing part of the view

Is there a way to black out a region (defined by a frame) of a UIImageView without creating an overlaying UIView instance (with this frame) and blacking that out?
Is there similarly a way to reveal part of a UIImageView without using UIView instances to black out the rest of the image?
I am going to cheat a bit here and suggest that you use layer as they are light weight.
CALayer *blackLayer = [CALayer layer];
blackLayer.backgroundColor = [UIColor blackColor];
blackLayer.frame = imageView.bounds;
[imageView.layer addSublayer:blackLayer];
For the second part you can consider using a grid of layers (black). When user touches the image view, you can pick the layers from the area he has touched and remove them from the super layer i.e. the image view's root layer.
you can subclass the UIIMageView and draw as per your needs in the drawRectMethod.
You can create a subclass of UIIMageView and have your definition of drawRectMethod over there.
You can also invoke the drawRectMethod at your will by calling setNeedsLayout method on the view .( setNeedsDisplay - causes drawRect to be called - . Only contentMode = UIViewContentModeRedraw will cause setNeedsDisplay to be called when a view's bounds changes).
UPDATE:
http://developer.apple.com/library/ios/#documentation/CoreGraphics/Reference/CoreGraphics_Framework/_index.html

Resources