limiting the touchable area in a UIButton in IPhone? - iphone

I have three custom buttons with non-rectangular images close to each other in my view. Then I have a problem with touchable area's of each button overlap with other buttons. So how can I limit the touchable area of each buttons to get the corresponding actions?

You can overwrite -pointInside:withEvent:, that internally will be used for hit testing.
A nice project using this technique is OBShapedButton, where transparent pixel will not trigger a hit.

Use two components. A UIImageView with a smaller UIButton on top.

You should create custom Type Buttons and add required images on each using this code:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn setFrame:frame];
Creating buttons with images this way will not result in overlapping images issues !!

Related

Why is it that the image I use for my nav_bar button has white edges?

I'm trying to use this image through interface builder, but it makes this weird white space on the outside. I cant change it in the program because of how everything is set up, or at least if I do it will involve rewriting the whole thing from scratch. Is there some way to add custom navigation bar buttons through IB without having this issue ??
UPDATE
Once I had added the button to the bar button I could customize it as needed, and achieve the result required, by changing the qualities of the button I added to Custom in IB.
Simply remove the rounded rect type from the UIButton declaration and write :-
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
instead of
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

iOS using image background for Button

i have that image background with transparent part and i went using for my buttons without
transparent part and if possible with interface builder
By the sounds of it you are possibly using the UIButtonTypeRoundedRect style, but UIButtonTypeCustom may be more suitable. You can change this within Interface Builder's inspector window.
still i refer this link http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
btnImage = [UIImage imageNamed:#"image.png"];
[btn setBackgroundImage:btnImage forState:UIControlStateNormal];
[btn setTitle:#"Title" forState:UIControlStateNormal];
or you can directly set background image from your xib by changing it into custom type.

How do I make a section of my UIView a clickable area

I want to make a certain section of my UIView clickable. When clicked, it triggers an IBAction, as would an UIButton.
How do I do this?
Add Custom UIButton on your view, what ever position you need to clickable, Then perform
action on it.
UIButton *button = [UIButton buttonWithType:0];
[button setFrame:CGRectmake(0,0,0,0)]; //What ever clickable area, provide here
[button addTarget:self action:#selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
Using UITapGestureRecognizer and put transparent button over the area where you want clickable section available
Add UITapGestureRecognizer to your UIView. When you receive a tap, check your recognizer's locationInView:. Trigger the action if the tap happened in the area that you would like to make tap-sensitive; otherwise, ignore the tap event.

IOS Custom UIButton

I currently work on a small application in which I have several UIButtons. Each of them has an UIImageView beneath it. At the moment, I associate each UIButton with each UIImageView (all are named via an IBOutlet - UIButton and UIImageView). When I press a button, the underlying image changes. I would like to know if it's possible (and how) to create a button which contains an underlying UIImage (not a UIButton image or background image), a sort of new object.
This image is slightly larger than the associated UIButton, like the apple calculator basic apps, and this image changes when I press the button.
I've tried several solutions (certainly incomplete) and I'm beginning to despair.
My current solution is functional but I do find it not particularly elegant.
UIButton has both image and backgroundImage as its properties.
To achieve what you are looking for, I would actually recommend you to stick to just a UIButton, and then using contentInset to position the image over the backgroundImage so it creates the effect I think you are looking for. This should be quite easy in interface builder...
you can make an invisible button and behind it make the image..
This will give you what you want but should be avoided at cost...
Stick to UIButton..
I believe even in calculator app they are only UIButtons and nothing else..
they have been coded to been momentary selected ..so that's why you see them kind of changing highlight for a small fraction and each button have been coded to perform specific function(+ - / equal .)
Update : i might be in doubt but if you asked for a button to have an image view inside it..
then here is the answer.
Make UIbutton. Make an ImageView and then call [theButton.view addSubview:ImageView];
Since both UIbutton and UIImage view inherit from UIView.. you can add as many subviews to them..that is inside them
If I understood your question correct then you may try the following:
UIButton *button = [UIButton alloc]init];
[button setImage:buttonImage forState:UIControlStateNormal]; when button not pressed
[button setImage:buttonImage forState:UIControlStateSelected]; when button tapped
[button release];

Standard UIButton with custom image

I'm trying to add a custom image on a standard UIButton. I know of the setBackgroundImage and setImage methods to do that. However, this will remove the 'standard' borders! Using UIButtonTypeCustom or UIButtonTypeRoundRect gives the same results...
Can this be so hard?
A normal button like those in the navigationbar - just a custom image instead of the titleLabel. Do I really have to draw the button itself in an image editor?
Yes you do have to draw a custom image.
Well you can add a subview of image to the UIButtonRoundRect. So just create a RoundRect button the usual way:
UIButton *button = [UIButton buttonWithType:UIButtonRoundRect];
then create an image view with your image and add it as subview to the button:
UIImageView *iv = [[UIImageView alloc] initWithImage:(UIImage *)image];
[button addSubivew:iv];
That should do the trick. Plus you can also set the position of that imageview by [iv setCenter:CGPointMake(x,y)];
Hope that helps.