Make a UIButton background image larger than frame - ios

I'm trying to make a UIButton with a background image where the image is larger than the button frame.
The problem is, the UIButton automatically scales the image to fit and haven't found a way to change the behavior.
Am I missing something obvious or is it time to make a custom button?

Subclass UIButton and override the
- (CGRect)backgroundRectForBounds:(CGRect)bounds;
method so it returns the size you want.
TIP: Use this method to center your background as well.

Looks like the answer to this one is. Not possible through existing interface on UIButton.
The simplest way to implement seems to be using a custom sub class of UIButton and creating a UIImageView with the same frame as the UIButton, turn off clipping and scaling and then just capture all the setBackground methods and capture state changes to handle "normal" button interaction changing the background images.

Related

Aspect Fill an image (the image will be updated by the user) in a Custom circular UIButton

I have a problem in one of my iOS app projects.
My question is: What is the most efficient way to create a circular UIButton with a specific size that will have an Aspect Fill UIImage, that the user can update anytime from the camera or library?
I'm trying to create a circular UIButton that will have the image that the user uploads. One solution I've tried is to make the image circular and then overlay it with just an empty button, but it doesn't feel too efficient.
I want to use these buttons in a UITableView, so basically more than one of these buttons.
I hope I can get an idea (not necessarily an example code, but more like a logic to make it more efficient and simple) of how to approach this problem with the UIButton so that the image will be updated and Aspect Filled within the properties of the button.
I too prefer the UIImageView with UITapGesture. Because by my experience the aspect ratio did not work well in UIButton. UIImageView with layer.cornerRadius with fine to execute from User Interation Side.
you can just add a UIButton in your UITableView and set/change backgroundimage of it with the UIButton click event.
turn the button into a circle.your can user code like this:
[btn.layer setMasksToBounds:YES];
[btn.layer setCornerRadius:10.0];
or if you use xib,you can do like this:
http://i.stack.imgur.com/Ytlqs.jpg

background image for CGContext

Question: How do I display a background image underneath a CGContext?
I have created a view controller using the storyboard and placed a view with a full-screen background image. Then, using the same view controller, I created a outlet to a UIView that has a CGContext where I draw a few lines.
Everything works except that the drawing takes place underneath the background image. (Suddenly I wonder if it's not the CGContext but the UIView that is the problem.)
I found a similar question here, but the solution makes it sound like the background image has to be constantly refreshed using CGContextDrawImage with a CGImageRef. Which may be true, but a static image being constantly refreshed doesn't sound like a very elegant solution. Thank you!
Create custom UIImageView subclass where you will draw your custom background.
Better to subclass UIImageView instead of UIView, because in this case you don't get problems with adding other views above this in Interface Builder.
Add UIImageView to your view controller, and change class to your custom class. Add all other views above.
Question: How do I display a background image underneath a CGContext?
Answer: Set the color of the view with the CGContext to Clear Color.
That way the background image is visible as well as the lines that are subsequently drawn. Guess I kind of overcomplicated that one.
Thank you for the response Vitaliy.

Touch Event clicking UIImageView while rotating

My ImageView rotates but while it rotates it doesn't recognize touches on itself.
Do you think it's okay if I create a button via code that's over the ImageView that recognizes the touch?
When an animation is applied on any UIView or any subclass object of a UIView like UIImageView, UIButton etc then it does not detect touch events because when an animation is applied to a view, the animated property changes to its end value right away. what you are actually seeing on screen is the presentation layer of your views layer.
To answer your question, Yes, you can make a UIButton that covers up the area of the UIImageView to detect touch events on it. That sounds like the easiest to implement option in this case.
Apart from that, this link may also help you in the process. Hit testing animating layers

Use a UIView as the background of a UIButton?

I have a UIButton. Rather than giving it a background Image, I want to use a custom UIView object as the background. I realize I could do this by rendering the UIView into an image, then using that. But is there a better way?
I've looked at playing with the Button's view tree, but that seems to have undesirable side effects.
To achieve the same, I usually do this:
Add subview to the parentView
Add a UIButton with clearColor to the parentView, with the same frame as the subview.
You can achieve the same by creating a custom UIView class that has this view and button as its properties.
If you want to use a UIView as a button, you should just add a tap gesture recognizer to the UIView and use that. Is there something specific you're looking for?

Adjusting UIButton's imageView properties

I'm having trouble figuring out how much I can adjust the imageView property of a UIButton. In the docs, it says:
Although this property is read-only, its own properties are read/write. Use these properties to configure the appearance and behavior of the button’s view.
I'm hoping to perform a transition where I shrink the size of the button in an animation, and I'd like the button's image to also shrink accordingly. However, I find that I have no control over the size of that image. If I modify either the frame or the bounds of UIButton's imageView property, nothing seems to happen. The image seems to want to retain it's standard dimensions. Changing autolayout properties on the imageView or contentMode properties on the button don't seem to help either.
In fact, the only thing that seems to work at all is to use UIEdgeInsetsMake to "squish" the image from all sides, but that isn't animatable.
I'd prefer not to use the backgroundImage property either, since I'm already using that to style the button in the first place.
You could add an own UIImageView as subview to the UIButton instead of using the "embedded" imageView.
You can set the frame of "your" image view as usually relative to the surrounding UIButton. You will have full control on it's position and you will also be able to animate it.

Resources