I have UINavigationBar with buttons than have been created using custom view. I added a target with selector for each buttom, but buttons have not respond to selector. Can you help me?
- (void)viewDidLoad
{
[super viewDidLoad];
[self.navigationItem setTitle:#"Title"];
// create custom view
container = [[UIView alloc] init];
[container setUserInteractionEnabled:TRUE];
NSString *path;
// create 2 buttons and put in a custom view
buttonList = [[UIButton alloc] init];
path = [[NSBundle mainBundle] pathForResource:#"dotActive" ofType:#"png"];
UIImage *imgList = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
path = [[NSBundle mainBundle] pathForResource:#"dotInactive" ofType:#"png"];
UIImage *imgListPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[buttonList setImage:imgListPrs forState:UIControlStateSelected];
[buttonList setSelected:TRUE];
[buttonList setFrame:CGRectMake(-30, 0, imgList.size.width, imgList.size.height)];
[buttonList setImage:imgList forState:UIControlStateNormal];
[container addSubview:buttonList];
[buttonList addTarget:self action:#selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonList setUserInteractionEnabled:TRUE];
buttonPic = [[UIButton alloc] init];
path = [[NSBundle mainBundle] pathForResource:#"dotActive" ofType:#"png"];
UIImage *imgPic = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
path = [[NSBundle mainBundle] pathForResource:#"dotInactive" ofType:#"png"];
UIImage *imgPicPrs = [[[UIImage alloc] initWithContentsOfFile:path] autorelease];
[buttonPic setImage:imgPicPrs forState:UIControlStateSelected];
[buttonPic setFrame:CGRectMake(-10, 0, imgPic.size.width, imgPic.size.height)];
[buttonPic setImage:imgPic forState:UIControlStateNormal];
[container addSubview:buttonPic];
[buttonPic addTarget:self action:#selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonPic setUserInteractionEnabled:TRUE];
// create UIBarButtonItem with a custom view
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];
// put item on navigation bar
[self.navigationItem setRightBarButtonItem:item];
}
Sorry Sveta,
I didn't see your complete code.Check this out.It will work for you.
- (void)viewDidLoad{
[super viewDidLoad];
[self.navigationItem setTitle:#"Title"];
// create custom view
UIView *container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 250, 30)];
[container setUserInteractionEnabled:TRUE];
// create 2 buttons and put in a custom view
UIButton *buttonList = [[UIButton alloc] init];
[buttonList setSelected:TRUE];
[buttonList setBackgroundColor:[UIColor redColor]];
[buttonList setFrame:CGRectMake(0, 0, 30.0, 30.0)];
[container addSubview:buttonList];
[buttonList addTarget:self action:#selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonList setUserInteractionEnabled:TRUE];
[buttonList release];
UIButton *buttonPic = [[UIButton alloc] init];
[buttonPic setFrame:CGRectMake(40.0, 0, 30.0, 30.0)];
[buttonPic setBackgroundColor:[UIColor blueColor]];
[container addSubview:buttonPic];
[buttonPic addTarget:self action:#selector(changeType:) forControlEvents:UIControlEventTouchUpInside];
[buttonPic setUserInteractionEnabled:TRUE];
[buttonPic release];
// create UIBarButtonItem with a custom view
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithCustomView:container];
// put item on navigation bar
[self.navigationItem setRightBarButtonItem:item];
}
- (void)changeType:(id)sender
{
NSLog(#"Change Type");
}
In Appdelegate add this code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
[navController release];
return YES;
}
I never did it programmatically.
Could you post your changeType?
It might looks like
-(void)changeType:(id)sender {
// doing something here
}
and should be declared in the interface section;
Also you could put a breakpoint on it to see if it called at all. And see the value of the variables. Please check if it called.
EDIT:
Below my code that you can insert into defaultly created Navigation-Based Application:
at RootViewController.m:
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *tmpInfoButton = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
self.navigationItem.rightBarButtonItem = tmpInfoButton;
[tmpInfoButton release];
}
-(void)buttonPressed:(id)sender {
NSLog(#"Button pressed...");
}
at RootViewController.h:
-(void)buttonPressed:(id)sender;
Related
I am creating a bar button in all classes. Is there anyway to write this code only once and be used in all classes?
toolbar = [[UIToolbar alloc]
initWithFrame:CGRectMake(90, 0, 100, 45)];
toolbar.tintColor=self.navigationController.navigationBar.tintColor;
// create an array for the buttons
NSMutableArray* buttonsArray = [[NSMutableArray alloc] initWithCapacity:2];
// create a standard save button
UIImage *buttonImage = [UIImage imageNamed:#"home.png"];
UIButton *homeButton = [UIButton buttonWithType:UIButtonTypeCustom];
[homeButton setImage:buttonImage forState:UIControlStateNormal];
homeButton.frame = CGRectMake(100,2, 35, 35);
[homeButton addTarget:self action:#selector(homeButtonpress) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *customHomeBarItem = [[UIBarButtonItem alloc] initWithCustomView:homeButton];
[buttonsArray addObject:customHomeBarItem];
[customHomeBarItem release];
UIImage *logoutImg=[UIImage imageNamed:#"power.png"];
UIButton *logOutBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[logOutBtn setImage:logoutImg forState:UIControlStateNormal];
[logOutBtn addTarget:self action:#selector(logOutButtonPress) forControlEvents:UIControlEventTouchUpInside];
logOutBtn.frame=CGRectMake(0, 2, 35, 40);
UIBarButtonItem *customLogOutButton=[[UIBarButtonItem alloc]initWithCustomView:logOutBtn];
[buttonsArray addObject:customLogOutButton];
[customLogOutButton release];
[toolbar setItems:buttonsArray animated:NO];
[buttonsArray release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
[toolbar release];
Create a Utility class
Add class method for creating & returning UIBarButtonItem.
Import Utility.h in .pch or in classes where you want to use it.
Utility.m
+ (UIBarButtonItem *) barButtonItemWithTitle:(NSString *)title backgroundImage:(NSString *)image target:(id)target action:(SEL)action
{
//create button & return it
return barButtonItem;
}
To use
ViewController.m
UIBarButtonItem* backButtonItem = [Utility barButtonItemWithTitle:#" Back" backgroundImage:#"back_button" target:self action:#selector(backButtonAction)];
self.navigationItem.leftBarButtonItem = backButtonItem;
I would like to create a NavigationBar with images as buttons on the right side of the NavigationBar.
Something like below Snapshot
How can I achieve this?
Hope This Helps
viewController.navigationItem.titleView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage.png"]];
UIBarButtonItem * item = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"yourimage2.jpg"]]];
viewController.navigationItem.rightBarButtonItem = item;
Here the Code, Just call below Methods From viewDidLoad method
- (void)addCustomButtonOnNavBar
{
UIBarButtonItem * item1= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"passImageNmae1"] style:UIBarButtonItemStylePlain target:self action:#selector(yourButtonAction1)];
UIBarButtonItem * item2= [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"passImageNmae2"] style:UIBarButtonItemStylePlain target:self action:#selector(yourButtonAction2)];
NSArray * buttonArray =[NSArray arrayWithObjects:item1,item2 ,nil];
self.navigationItem.rightBarButtonItems =buttonArray;
}
UIView *viewWithButtons = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 44)];
//view customization
UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:leftButton];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
//Button customiztaion
[viewWithButtons addSubview:rightButton];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:viewWithButtons];
[viewWithButtons release];
I use a category for this:
UIBarButtonItem+WithImageOnly.h:
#import <UIKit/UIKit.h>
#interface UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage*)image target:(id)target action:(SEL)action;
#end
UIBarButtonItem+WithImageOnly.m:
#import "UIBarButtonItem+WithImageOnly.h"
#implementation UIBarButtonItem (WithImageOnly)
- (id)initWithImageOnly:(UIImage *)image target:(id)target action:(SEL)action {
CGRect frame = CGRectMake(0, 0, image.size.width, image.size.height);
frame = CGRectInset(frame, -5, 0);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button setImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
return [self initWithCustomView:button];
}
#end
Usage:
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImageOnly:[UIImage imageNamed:#"image"] target:self action:#selector(someAction:)]
this is my code
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *segmentTextContent = [NSArray arrayWithObjects:
NSLocalizedString(#"button1", #""),
NSLocalizedString(#"button2", #""),
NSLocalizedString(#"button3", #""), nil];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] nitWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 400, 30);
self.navigationItem.titleView = segmentedControl;
[segmentedControl release];
}
three buttons are displayed but i don't know how to set action in button help me thanks in advance
add target at the time of segmentedControl creation like below
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
in that selector
-(IBAction) segmentAction:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
switch (segmentedControl.selectedSegmentIndex) {
case 0:
self.segmentLabel.text =#"Segment 1 selected.";
break;
case 1:
self.segmentLabel.text =#"Segment 2 selected.";
break;
default:
break;
}
You have to make 3 buttons and add them to an UIToolbar.
- (void)viewDidLoad
{
[super viewDidLoad];
// create a toolbar to have the buttons at the right side of the navigationBar
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 44.01)];
toolbar.tintColor = [UIColor clearColor];
[toolbar setTranslucent:YES];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// Create button1
UIBarButtonItem *button1 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:#selector(button1Pressed)];
[buttons addObject:button1];
[button1 release];
// Create button2
UIBarButtonItem *button2 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:#selector(button2Pressed)];
[buttons addObject:button2];
[button2 release];
// Create button3
UIBarButtonItem *button3 = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:#selector(button3Pressed)];
[buttons addObject:button3];
[button3 release];
// stick the buttons in the toolbar
[toolbar setItems:buttons animated:NO];
//self.toolbarItems = buttons;
[buttons release];
// and put the toolbar in the nav bar
[[self navigationItem] setRightBarButtonItem:[[[UIBarButtonItem alloc] initWithCustomView:toolbar] autorelease]];
[toolbar release];
}
...
- (void)button1Pressed
{
//do stuff
}
- (void)button2Pressed
{
//do stuff
}
- (void)button3Pressed
{
//do stuff
}
I'm trying various ways of changing a UIBarButtonItem's image once it has been pressed, without any luck.
// bookmarkButton is a property linked up in IB
-(IBAction)bookmarkButtonTapped:(id)sender
{
NSLog(#"this action triggers");
// attempt 1
UIBarButtonItem* aBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"bookmarkdelete.png"] style:UIBarButtonItemStylePlain target:self action:#selector(bookmarkButtonTapped:)];
bookmarkButton = aBarButtonItem;
[aBarButtonItem release];
// attempt 2
bookmarkButton.image = [UIImage imageNamed:#"bookmarkdelete.png"];
}
Is there another way to do this?
The toolbar contains an array - items - as a property. So after setting up the toolbar as an IBOutlet property, I had to insert a new button into that array.. like this:
NSMutableArray *items = [[NSMutableArray alloc] initWithArray:self.toolBar.items];
UIBarButtonItem *newButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"newButton.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonTapped:)];
[items replaceObjectAtIndex:0 withObject:newButton];
self.toolBar.items = items;
[newButton release];
[items release];
Is bookmarkButton a UIButton? Should it be referenced via (UIButton *)aBarButtonItem.customView instead of directly?
If it is a UIButton then you'll want to set the image based on state: - (void)setImage:(UIImage *)image forState:(UIControlState)state
Note that there is also a setBackgroundImage with the same API if you want that instead.
This should work
UIImage *normalButtonImage = [UIImage imageNamed:#"TableViewIcon"];
UIImage *selectedButtonImage = [UIImage imageNamed:#"CollectionViewIcon"];
CGRect rightButtonFrame = CGRectMake(0, 0, normalButtonImage.size.width,
normalButtonImage.size.height);
UIButton *rightButton = [[UIButton alloc] initWithFrame:rightButtonFrame];
[rightButton setBackgroundImage:normalButtonImage forState:UIControlStateNormal];
[rightButton setBackgroundImage:selectedButtonImage forState:UIControlStateSelected];
[rightButton addTarget:self action:#selector(toggleTableView:)
forControlEvents:UIControlEventTouchDown];
self.toggleMediaView = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
[self.navigationItem setLeftBarButtonItem:self.toggleMediaView];
self.navigationItem.leftBarButtonItem.enabled = NO;
try [bookmarkButton setImage:[UIImage imageNamed:#"bookmarkdelete.png"] forState:UIControlStateNormal];
i got the following code:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"star.png"] style:UIBarButtonItemStylePlain target:self action:#selector(buttonFavoriteClicked:)];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
but my button looks still like a Button with UIBarButtonItemStyleBordered
is there a way to set a button with plain style at this position?
Create a UIButton in interface builder, make it look like exactly what you want. Create an outlet for in in your .h:
IBOutlet UIButton * _myButton;
Then set it as your right bar button item using a custom view, which will eliminate the border:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_myButton];
This works because UIButton is a subclass of UIView.
Try this instead:
- (id)init {
if (self = [super init]) {
self.title = #"please wait";
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[button setImage:[UIImage imageNamed:#"star.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonFavoriteClicked) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *favorite = [[UIBarButtonItem alloc] initWithCustomView:button];
[button release];
self.navigationItem.rightBarButtonItem = favorite;
}
return self;
}
Hope it helps.
Or do this in code without IB:
// add setting button to nav bar
UIImage* settingsGearImg = [UIImage imageNamed:#"settings_gear.png"];
CGRect frameimg = CGRectMake(0, 0, settingsGearImg.size.width, settingsGearImg.size.height);
UIButton *settingsButton = [[UIButton alloc] initWithFrame:frameimg];
[settingsButton setBackgroundImage:settingsGearImg forState:UIControlStateNormal];
[settingsButton addTarget:self action:#selector(settingsButtonAction) forControlEvents:UIControlEventTouchUpInside];
[settingsButton setShowsTouchWhenHighlighted:YES];
UIBarButtonItem *settingButtonItem =[[UIBarButtonItem alloc] initWithCustomView:settingsButton];
self.navigationItem.leftBarButtonItem = settingButtonItem;
Results in:
When using this icon image (it's white on a white background so isn't visible here - link is: http://i.stack.imgur.com/lk5SB.png):
It's weird but it works. Say "No" to images/outlets! You shouldn't even set the UIBarButtonItemStylePlain property. The trick is to place button into UIToolBar with some special attributes:
UINavigationItem *item = [[UINavigationItem alloc] initWithTitle:#"Cool plain bar"];
UIToolbar *tools = [[UIToolbar alloc]
initWithFrame:CGRectMake(0.0f, 0.0f, 44.0f, 44.01f)];
tools.clipsToBounds = NO;
tools.barStyle = -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:1];
UIBarButtonItem *bi = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refreshTableView)];
[buttons addObject:bi];
[tools setItems:buttons animated:NO];
UIBarButtonItem *rightButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
item.rightBarButtonItem = rightButtons;
item.hidesBackButton = YES;
[myCoolNavigationBar pushNavigationItem:item animated:NO];
Although Frank is right, this code should be in viewDidLoad, the issue here is what BarButtons look like. If you want it to look different, you need to use a different type of UIView. You can add a UIButton to a UIToolbar just fine.
-(void)viewDidLoad {
[super viewDidLoad];
self.title = #"please wait";
self.navigationItem.rightBarButtonItem = [[[UIButton alloc] init] autorelease];
}
Edit:
Sorry, you wanted a plain UIBarButtonItem. I don't believe you can do this with a UINavigationBar. You could try adding a UserInteractionEnabled UILabel as the rightBarButtonItem, I'm not sure if it will work or not.
Seems this isn't currently possible.
sbwoodside has a solution.
Why not try UI7Kit? Works well for me, easy to use.
UIImage *img = [UIImage imageNamed:#"white_star.png"];
CGSize itemSize = CGSizeMake(20, 20);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[img drawInRect:imageRect];
img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIBarButtonItem *barButton = [[UIBarButtonItem alloc ] initWithImage:img style:UIBarButtonSystemItemAdd target:self action:#selector(favoriteButtonClicked)];
barButton.style = UIBarButtonItemStyleBordered;
Try this,
UIBarButtonItem *barBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:#"more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(didPressButton)];