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

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.

Related

How to Better Drag a UIButton? UIButton UIControlEventTouchUpInside Event Is Not Called

My draggable button is using these events to be dragged:
[button addTarget:self action:#selector(beganDragging:)
forControlEvents:UIControlEventTouchDown];
[button addTarget:self action:#selector(wasDragged:withEvent:)
forControlEvents:UIControlEventTouchDragOutside | UIControlEventTouchDragInside];
[button addTarget:self action:#selector(exitDragging:)
forControlEvents:UIControlEventTouchUpInside];
It's straight forward above. Method wasDragged adjusts the center of the UIButton to the UITouch location. However, there are cases when I end up with button being hung (normally it snaps to grid after finger release) because UIControlEventTouchUpInside is never called!
How? Let's say I start dragging a button and while holding it I make a screenshot (Power+Home). When I release the button no events are ever called (i.e. UIControlEventTouchUpInside). Therefore, my button is stuck and not snapping to grid which is in method exitDragging
Any light how to make sure exitDragging always runs?
Maybe it is easier using the UIView APIs with touchesBegan, touchesEnded etc.
It seems you are anyway not using the UIButton functionalities. I think that managing a drag with the target mechanism of UIButton is not what it was intended for.

How to add button to MBProgressHUD, relative to text label?

I want to add a custom cancel button to MBProgressHUD, and I want it to be just right of text label. But MBProgresHUD is drawn by overriding drawRect method, so, while I can add button as subview to HUD, i don't know how large text label will be so I can position my button properly.
How can this be achieved?
I'm not sure this is possible directly using MBProgressHUD. Not without rewriting much of it.
One option could be to just add your own button as a subview of the view you are adding the HUD to that would just close the HUD or cancel the action.
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(50, 50, 50, 50)]; // made up coords here
// set button stuff here
[self.view addSubview:button];
You will need to set the button actions, background or image (for look and feel), then add it to your view. Basically 2 views to achieve what you need. HUD and button. Probably a lot easier than rewriting MBProgressHUD to allow buttons.

UIButton in navigaton bar like Tweetbot

I would like to reproduce a Tweetbot like navigation bar.
I'm still searching if it's possible to place an UIButton in place of title in an UINavigationBar bar and to make it fits perfectly between right and left buttons like it is done in Tweetbot application.
When I try to do this, over a certain size the self.navigationItem.titleView is resized down automatically
Tell me if I miss something evident,
Thank you
I provide two screenshots to let you see what I'm talking about
They've probably rolled in their own implementation of UINavigationBar and/or UINavigationController. I guess it's not worth trying to hack UIKit that much, since it will be very fragile and not future-proof.
You can add a button to the navigation item's title view property, which accepts an UIView, and since a UIButton is a subclass of an UIView this is not an issue.
Alternatively, if you are using storyboard, just drag a button to the center of the navigation bar, it's that easy.
// Create the button, and specify the type
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// Set the button's frame
button.frame = CGRectMake(0.0f, 0.0f, 30.0f, 30.0f);
// Set your custom image for the button
[button setImage:[UIImage imageNamed:#"YOUR_IMAGE_NAME"] forState:UIControlStateNormal];
// Set the button's action, which is the method that will be called when it is pressed
[button addTarget:self action:#selector(doSomething:) forControlEvents:UIControlEventTouchUpInside];
// Add the button to the navigation bar's title view
[self.navigationItem.titleView addSubview:button];
SDK Reference:
https://developer.apple.com/library/ios/#documentation/UIKit/Reference/UINavigationItem_Class/Reference/UINavigationItem.html
Have you try to set autoresizesSubviews to YES on titleView, then set the correct autoresizing masks on the UIButton? If this doesn't work I suggest you to create your custom view subclass and override -layoutSubviews to fit your needs.

limiting the touchable area in a UIButton in 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 !!

UIButton in non-visible area of UIScrollView

How can I get a button tap for a button which is located in a UIScrollView, but in its initially non-visible area?
Solved
Problem was that the content was visible within a non-clipped view - therefore not getting touches.
I think you should add an action (yourAction in the code below) to the UIButton.
programmatically:
[button addTarget:self action:#selector(yourAction:) forControlEvents:UIControlEventTouchUpInside];
-(void)yourAction:(id)sender{
// your code
}
With the gui builder you should connect your UIButton to an action defined as IBAction in your .h controller file:
-(IBAction)yourAction:(id)sender;