I am new developer in iPhone.
I want to create uibutton.
When I click the button, then the button will be flash like as below.
How to create the flash, when I click my button?
There is a property of UIButton named showsTouchWhenHighlighted. If you set the property to YES, it will show the glow when you touch the button. It's default value is NO.
[yourButton setShowsTouchWhenHighlighted:YES];
What you do is set the highlighted image. For example:
[someButton setImage:[UIImage imageNamed:#"MyHighlight.png"]
forState:UIControlStateHighlighted];
Then when a user taps it, your MyHighlight.png will show.
toolbar = [UIToolbar new];
toolbar.barStyle = UIBarStyleDefault;
[toolbar sizeToFit];
toolbar.frame = CGRectMake(0, 410, 320, 50);
//Add buttons
UIBarButtonItem *systemItem1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(pressButton1:)];
UIBarButtonItem *systemItem2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction
target:self
action:#selector(pressButton2:)];
UIBarButtonItem *systemItem3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:#selector(pressButton3:)];
//Use this to put space in between your toolbox buttons
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
//Add buttons to the array
NSArray *items = [NSArray arrayWithObjects: systemItem1, flexItem, systemItem2, flexItem, systemItem3, nil];
//release buttons
[systemItem1 release];
[systemItem2 release];
[systemItem3 release];
[flexItem release];
//add array of buttons to toolbar
[toolbar setItems:items animated:NO];
[self.view addSubview:toolbar];
use this to create toolBar.
and use this for search button
UIBarButtonSystemItemSearch
If you want to change the button image when you clicked on button now set different images for both default and highlighted stats. As:
[tempButton setImage:[UIImage imageNamed:#"NormalImage.png"]
forState:UIControlStateNormal];
[tempButton setImage:[UIImage imageNamed:#"HighlightImage.png"]
forState:UIControlStateHighlighted];
If you want the flash effect for the same image, when button is clicked on,
In your XIB, Goto Inspector window > control > check the Highlighted property.
You can try this by code as:
[tempButton setShowsTouchWhenHighlighted:YES];
//or
tempButton.highlighted = YES;
Related
My problem is following: i want to know the coordinates to an item of a toolbar so that i could pop some data right there. These barbuttonitems never reveal me there frame properties.
Is there a solution?
You can use if you want to give spacing.
UIBarButtonItem *fixedLeftSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
// for adding Custom view
UIButton *webButton = [UIButton buttonWithType:UIButtonTypeCustom];
[webButton setFrame:CGRectMake(0, 0, 50, 40)];
[webButton setImage:[UIImage imageNamed:#\"webIcon.jpg\"] forState:UIControlStateNormal];
[webButton addTarget:self action:#selector(goToWebPage) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *webpageBtn = [[UIBarButtonItem alloc]initWithCustomView:webButton];
[webpageBtn setTarget:self];
[webpageBtn setAction:#selector(goToWebPage)];
[toolbar setItems:[NSArray arrayWithObjects: webpageBtn,fixedLeftSpace,webpageBtn, nil]];
Also see the link.
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 210, 44)];
UIBarButtonItem *space = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
NSMutableArray* buttons = [[NSMutableArray alloc] initWithObjects:bt1,space,bt2,space,bt3, nil];
[tools setItems:buttons animated:YES];
UIBarButtonItem *toolsBtn = [[UIBarButtonItem alloc]initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = toolsBtn;
If you are creating a toolbar from xib file then you need to add multiple "flexible space bar button item" before and after the buttons if you multiple bar buttons on toolbar until you get the desired spacing.
If you are creating UIToolbar dynamically then you need to add flexible buttons dynamically.
I have added array of bar button to the navigation items using the property rightBarButtonItems,it work good for iOS5,when i tested in iOS6 only one bar button item is visible.
UIBarButtonItem *updateButton = [[UIBarButtonItem alloc]
initWithTitle:#"Update"
style:UIBarButtonItemStylePlain
target:self
action:#selector(updateData)];
UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc]
initWithTitle:#"Refresh"
style:UIBarButtonItemStylePlain
target:self
action:#selector(refresh)];
NSArray *arrBtns = [[NSArray alloc]initWithObjects:updateButton,refreshButton, nil];
self.navigationItem.rightBarButtonItems=arrBtns;
Is there any new property for iOS6 to add the array of bar button to navigationitem.
Any help would be appreciated,Thanks a lot.
Please use the segmentController on the rightBarButtonItems if you want to add multiButton on rightBarButtonItems of NavigationBar
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"Add",#"Delete",
nil]];
segmentedControl.frame = CGRectMake(0, 0, 80, 30);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setWidth:35.0 forSegmentAtIndex:0];
[segmentedControl setWidth:45.0 forSegmentAtIndex:1];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.leftBarButtonItem = segmentBarItem;
[segmentBarItem release];
Secondly add the second button on other side of the first bar Button.
I try to set toolbar items in the navigationcontrollers top view. Seems to work in the subviews... but why not in the top view...any ideas? I get the add button... but not my custom button.
- (void)configureToolbarItems {
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self action:#selector(addNewTaskButtonPressed)];
//Green button
greenButton=[app makeGreenButton:self];
UIBarButtonItem *greenBarButton = [[UIBarButtonItem alloc] initWithCustomView:greenButton];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// Set our toolbar items
[self setToolbarItems:[NSArray arrayWithObjects:
addButtonItem,flexibleSpace, greenBarButton, nil] animated:YES]; }
This is the makeButton procedure... works fine in other views:
-(UIButton*)makeGreenButton:(UIViewController*)caller {
UIButton *greenButton;
//load the image for yellow button
UIImage *greenButtonImage = [UIImage imageNamed:#"greenButton.png"];
//create the button and assign the image
greenButton = [UIButton buttonWithType:UIButtonTypeCustom];
[greenButton setImage:greenButtonImage forState:UIControlStateNormal];
greenButton.showsTouchWhenHighlighted=TRUE;
//set the frame of the button to the size of the image (see note below)
greenButton.frame = CGRectMake(0, 0, greenButtonImage.size.width*2, greenButtonImage.size.height*2);
//Add target
[greenButton addTarget:caller action:#selector(greenButtonReleased:) forControlEvents:UIControlEventTouchUpInside];
return greenButton; }
I have a View With UINavigationBar
Navigation Bar only permits me the button at fix position
With Fix Position at the sides of the navigation bar..
I want to customize the position of button...
Any Idea...will help me
Thanks in Advance
You can't customize the position of the buttons on a UINavigationItem, you only can set the rightBarButtonItem and leftBarButtonItem.
If you really need this, consider using a toolbar instead.
If you need a back button just like the navigation bar's back button, create a custom button and use an image.
Here's a PSD that will help.
You can position custom buttons within the UINavigationItem. This is how I added three buttons to the right:
// create a toolbar to have three buttons in the right (thanks Mart!)
tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 157, 44.01)];
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 1.0, 157.0, 44.1)];
[imgView setImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:#"NavigationBarBackground" ofType:#"png"]]];
[tools addSubview:imgView];
[tools sendSubviewToBack:imgView];
[tools setTintColor:[UIColor colorWithRed:127/255.0 green:184/255.0 blue:72/255.0 alpha:1.0]];
[imgView release];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithTitle:#"Filter" style:UIBarButtonItemStyleBordered target:self action:#selector(showFilter:)];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"Map.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(showMap:)];
[buttons addObject:bi];
[bi release];
bi = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"Favourite.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(saveSearch:)];
[buttons addObject:bi];
[bi release];
[tools setItems:buttons animated:NO];
[buttons release];
rightBarButton = nil;
rightBarButton = [[UIBarButtonItem alloc] initWithCustomView:tools];
self.navigationItem.rightBarButtonItem = rightBarButton;
Here is my code. This is adding image but images are being appeared in navigation bar.
UIToolbar* toolbartop = [[UIToolbar alloc]
initWithFrame:CGRectMake(0, 0, 100, 45)];
[toolbar setBarStyle: UIBarStyleBlackOpaque];
// create an array for the buttons
NSMutableArray* buttonstop = [[NSMutableArray alloc] initWithCapacity:5];
UIBarButtonItem *remainderButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(LoadOption:)];
[buttonstop addObject:remainderButton];
[remainderButton release];
UIBarButtonItem *faveButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
[buttonstop addObject:faveButton];
[faveButton release];
// put the buttons in the toolbar and release them
[toolbartop setItems:buttonstop animated:NO];
[buttonstop release];
// place the toolbar into the navigation bar
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbartop];
But this code is showing image in navigation right bar.
UIBarButtonItem *settingButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"home.png"]
style:UIBarButtonItemStylePlain
target:self
action:#selector(homeAction)];
self.navigationItem.rightBarButtonItem = settingButton;
Why left navigation bar button is not showing images?
[toolbartop release];
You can add the image to your left BarButtonItem as follows:
UIButton *button1 = [[UIButton alloc] init];
button1.frame=CGRectMake(0,0,105,30);
[button1 setBackgroundImage:[UIImage imageNamed: #"image1.png"] forState:UIControlStateNormal];
[button1 addTarget:appDelegate action:#selector(Open_Link1) forControlEvents:UIControlEventTouchUpInside];
UIButton *button2 = [[UIButton alloc] init];
button2.frame=CGRectMake(105,0,105,30);
[button2 setBackgroundImage:[UIImage imageNamed: #"image2.png"] forState:UIControlStateNormal];
[button2 addTarget:appDelegate action:#selector(Open_Link2) forControlEvents:UIControlEventTouchUpInside];
UIView *viewButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 210, 30)];
[viewButtons addSubview:button1];
[viewButtons addSubview:button2];
[button1 release];
[button2 release];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:viewButtons];
[viewButtons release];
Here please make sure that the methods "Open_Link1" and "Open_Link2" must exists in the code as follows:
-(void)Open_Link1
{
// Write your logic
}
-(void)Open_Link2
{
// Write your logic
}
Let me know if you want more help.
The left bar button item is automatically used by the framework for showing a "Back" button. You can't put anything here, at least not when your navigation bar is managed by a UINavigationController. For this to work you have to handle the UINavigationBar yourself.
Edit:
in your leftBarButtonItem, try putting a UISegmentedControl instead of a UIToolbar for toolbartop.
You are adding a button to toolbar but you aren't adding your toolbar to any view. Try to set the button directly using
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:buttonStop];