Setup Sharekit's share button with UIButton, not UIBarButton - ios

In ShareKit's install instructions here: http://getsharekit.com/install/
they say to create a share button by putting this in the .m
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(share)]
How can I set up a share button with just a UIButton, not a UIBarButton?

Create your UIBUtton normall.
Then for the addTarget method use share
so
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(.....
[btn addTarget:self action:#selector(share) forControlEvents:UIControlEventTouchUpInside];

Related

How to get this UIBarButtonItem in the picture?

I want to get a button with short lines on UINavigationBar.
Please, see that on this image:
What is it? How to get it?
You'll want to instantiate a UIBarButtonItem with a custom view.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake(0, 0, 24, 24);
[button setImage:[UIImage imageNamed:#"your_image_filename"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonTappedMethod) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setLeftBarButtonItem:barButtonItem];
This creates a UIButton with a supplied image. That UIButton is then set as a custom view for a UIBarButtonItem. Then, just set the barButtonItem on your navigation item.

Make custom Navigation Buttons glow when touched?

In my navigation bar I have two right button bar items. One is a custom button and the other uses the info light button type. Both work great but I would like to make the custom button have the default glow the same way the info button does when you touch it. Is this possible?
// Default info light btn
UIButton *infoBtn = [UIButton buttonWithType:UIButtonTypeInfoLight];
UIBarButtonItem *infoBtnItem = [[UIBarButtonItem alloc]initWithCustomView:infoBtn];
// Custom Btn
UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
[customButton setFrame:CGRectMake(0, 0, btnImage.size.width, btnImage.size.height)];
[customButton setBackgroundImage:btnImage forState:UIControlStateNormal];
[customButton addTarget:self action:#selector(rightDrawerButtonPress:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customButtonItem = [[UIBarButtonItem alloc] initWithCustomView:wlButton];
NSArray *rightBtns = #[customButtonItem, infoBtnItem];
[navItem setRightBarButtonItems:rightBtns animated:YES];
you just need to put one more line of code like:-
customButton.showsTouchWhenHighlighted = TRUE;
After this your code Output something like this:-

Info button as UIBarButtonItem doesn't respond to taps

I'm trying to programmatically add and info button to my app's navigation. However, it doesn't ever fire the action that I'm registering to it.
UIBarButtonItem* infoButton =[[UIBarButtonItem alloc] initWithCustomView:[UIButton buttonWithType:UIButtonTypeInfoLight]];
[infoButton setTarget:self];
[infoButton setAction:#selector(infoButtonTapped)];
The button shows up in my nav bar, but clicking on it doesn't pass into infoButtonTapped
Add the target/action to the UIButton. The bar button item behaves differently when it's set up from a UIButton.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(infoButtonTapped) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *infoButton = [[UIBarButtonItem alloc] initWithCustomView:button];

UIBarButtonItem Custom view in UINavigationBar

I am making a custom bar buttons for uinavigationbar, I am using following code
UIImageView *backImgView= [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"chk_back.png"]]; [backImgView setUserInteractionEnabled:YES];
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backImgView];
[backButton setTarget:self];
[backButton setAction:#selector(BackBtn)];
checkIn_NavBar.topItem.leftBarButtonItem = backButton;
The problem is it is not calling its action. What i am doing wrong?
From Apple documentation:
Initializes a new item using the specified custom view.
- (id)initWithCustomView:(UIView *)customView
Parameters
customView
A custom view representing the item.
Return Value
Newly initialized item with the specified properties.
Discussion:
The bar button item created by this method does not call the action method of its target in response to user interactions. Instead, the bar button item expects the specified custom view to handle any user interactions and provide an appropriate response.
Solution: "Create button with your background image (set action to this button) and init bar button with this button". For example:
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
btn.frame = CGRectMake(0,0,25,25)
[btn setBackgroundImage:[UIImage imageNamed:#"chk_back.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(BackBtn) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc] initWithCustomView:btn];
I accomplished this using following code:
UIButton *nxtBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[nxtBtn setImage:[UIImage imageNamed:#"chk_next.png"] forState:UIControlStateNormal];
[nxtBtn addTarget:self action:#selector(NextBtn) forControlEvents:UIControlEventTouchUpInside];
[nxtBtn setFrame:CGRectMake(0, 0, 64, 31)];
UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithCustomView:nxtBtn];
checkIn_NavBar.topItem.rightBarButtonItem=nextButton;

Dismiss ModalView Programmatically by done button

I have to click on done button to dismiss modalview programmatically. I think UIButton is better than UIBarButtonItem to add UIControlEventsTouchupInside.
But with UIButton I'm confused what buttontype should use.
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoLight];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem * button = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(dismissViewaction:)] autorelease];
You will most likely want to use UIButtonTypeRoundedRect or UIButtonTypeCustom
With the custom type, you can add images for display.
You can try "stealing" these images from the UIBarButtonItem (image property defined in UIBarItem) and making the custom button look like the UIBarButtonSystemItemDone button
UIBarButtonItem * buttonForImage = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:nil action:nil] autorelease];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:#selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside];
[button setImage:buttonForImage.image forState:UIControlStateNormal];
Things to look out for. When setting an image for a UIButton, it does not scale to the size of the button according to contentMode property. If you want the image to follow the rules of the contentMode property, use setBackgroundImage: forState: instead.
I think the type of button doesn't matter. You have to assign an action or a selector to the button.
Something like this:
UIBarButtonItem *bttItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(yourBttAction:)] autorelease];
the action:
- (IBAction) yourBttAction:(id)sender
{
NSLog(#"Done Button clicked");
//do something
}
If the button is on the modalViewController, I usualy, for dismiss it, I use:
[self dismissModalViewControllerAnimated:(BOOL)];
or
//if you have a navigationController
[self.navigationController dismissModalViewControllerAnimated:(BOOL)];
But if you want to use a delegate to dismiss it, take a look at this tutorial

Resources