Two buttons needed at navigationItem.rightBarButtonItem. Image not getting displayed - iphone

I have to have two buttons on the right side of the navigation bar. So i created a tool bar and added two bar button items to its content view. Like below.
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc]
initWithImage:[UIImage imageNamed:#"share-icon-32x32.png"]
style:UIBarButtonItemStyleBordered
target:self action:#selector(showShare)];
shareButton.width = 30.0f;
[buttons addObject:shareButton];
[shareButton release];
where buttons is an array holding the button objects.
Like wise i have another barbutton item like below
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(addDescription)];
addButton.style = UIBarButtonItemStyleBordered;
[buttons addObject:addButton];
[addButton release];
Now add the array to the toolbar like below
[toolbar setItems:buttons animated:YES];
[buttons release];
and add it to the rightBarButton like below
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]
initWithCustomView:toolbar];
[toolbar release];
I am getting two bar button items on the right bar but i am not able to get the image displayed on to the share button item. Its just a white patch. Can any body tell me what am i doing wrong or how to get the image displayed.
Regards,
Syed Yusuf

From the docs for initWithImage:style:target:action::
The alpha values in the source image are used to create the images—opaque values are
ignored.
This means that UIKit uses the alpha channel of your image, not the RGB pixel values. The image you provided is probably completely opaque (has no transparency) and initWithImage:style:target:action: interprets this as a white square. You must save your image with an alpha channel that defines the shape of your icon.

Related

UIBarButtonItem appearance and setBackButtonBackgroundImage

I change my back button when pushing a new viewcontroller in my navigationcontroller. But it doesnt look nice and its stretched. Also, how can I remove the "News" title in my back button?
here's my code. see the image
and the code is
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"back_button.png"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
I want to achieve something like this http://i228.photobucket.com/albums/ee262/romano2717/photo4.png
Use the following code to put the image in position.
int imageSize = 20; //REPLACE WITH YOUR IMAGE WIDTH
UIImage *barBackBtnImg = [[UIImage imageNamed:#"NavBackButton"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, imageSize, 0, 0)];
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:barBackBtnImg
forState:UIControlStateNormal
barMetrics:UIBarMetricsDefault];
You can use resizableImageWithCapInsets to specify which pixels should not be stretched. If you put your image-width in there it won't stretch your image.
You don't have to put it in the .m file btw. (You could use the appDelegate.m file) just make sure it get's called before the NavigationBar is drawn.
Unless I'm missing something, you have to create the UIBarButtonItem in code to remove or change the title, so it would only apply to the view controllers you put the code in:
UIBarButtonItem *myBackButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"back_button.png"] style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = myBackButton;
[myBackButton release];
The reason why the code you posted works that way is because it only sets the background and the documentation does state that:
For good results, backgroundImage must be a stretchable image.
You can create custom Button and add it as a bar button to remove the stretch. or you can get graphics of back button size.
to remove the title of the button you have to set self.title = ""; in viewWillDisappear method and set title again when in viewWillAppear method.
hope this will solve your issue.
Use youparentViewController.title = #" ";
I did have the same problem with the title
Below code will Remove Title from the Back button.
UIBarButtonItem *theBackButton = [[UIBarButtonItem alloc] initWithTitle:#"" style:UIBarButtonItemStylePlain target:self action:#selector(yourAction)];
[[UIBarButtonItem appearance] setBackBarButtonItem:theBackButton];
[theBackButton release];

How can i add a next and previous button at the segmented Controller on a navigation bar in iphone application development?

I am in great trouble....How can i set next and previous button/arrow at my segmented bar...if anyone need brief about my problem then please see this link...How can i add a next and previous button at the segmented Controller?
i have attached an image to understand the problem...so anybody help me please....
NOTE THAT: In my current project it has more than 5 buttons to add at the segmented bar so when i will press next/previous arrow then segmented bar should be move from his place.If my question is not clear to you then please see my another link....
Thanks in Advance
EDIT:
UIBarButtonItem *previousBarButtonItem = [[UIBarButtonItem alloc] //init];
initWithTitle:#"<"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(previousBarButtonAction:)];
self.navigationItem.leftBarButtonItem = previousBarButtonItem;
[previousBarButtonItem release];
UIBarButtonItem *nextBarButtonItem = [[UIBarButtonItem alloc] //init];
initWithTitle:#">"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(nextBarButtonAction:)];
self.navigationItem.rightBarButtonItem = nextBarButtonItem;
[nextBarButtonItem release];
//This Portion For UIToolbar
topToolBar = [UIToolbar new];
topToolBar.barStyle = UIBarStyleDefault;
[topToolBar sizeToFit];
topToolBar.frame = CGRectMake(50, 410, 280, 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
[topToolBar setItems:items animated:NO];
self.navigationItem.titleView = topToolBar;
this is my current coding position now i have 4 buttons in the uitoolbar but only 3 button can see so i want to move this toolbar when i will press next or previous button to see the others button whose are out of frame of uitoolbar??
EDIT:
I able to scroll the navigation bar item using uiview animation but now my problem is when i press the next/prev button then it is moving from the current place according to the changing of the coordinate of the uitoolbar and moving over the pre/next baritem frame whose are not in the uitoolbar items. but it should be wothin a uiview and should change the coordinate within the uiview not out of the view...now tell me what can i do for this problem.
Firstly in figure the NavigationBar you are seeing is actually UIToolBar. Unfortunately it is not possible to add anymore controls on the UINavigationBar. But you can achieve exactly same UI with UIToolBar where you can add any controls.
So to achieve this use UIToolBar and not UINavigationBar. Also use UIBarButtonItem with custom title to achieve Next and Previous functionality.
EDIT
Here are few links for example of UIToolBar
http://www.codeproject.com/Articles/43658/How-to-Make-a-Toolbar-with-UIToolbar
http://osmorphis.blogspot.com/2009/05/multiple-buttons-on-navigation-bar.html
http://atastypixel.com/blog/making-uitoolbar-and-uinavigationbars-background-totally-transparent/
But this all explains using codes. Instead using Interface Builder, it becomes too easy to use UIToolBar (if coding is not so important).
I think this is being slightly overcomplicated- can you not just set the text of the two UIBarButtonItems to "<" and ">"?

Subclassing UINavigationController to have extra icons for rightbarbuttonitem and leftbarbuttonitem

I'm looking at some other iPad apps that have the capability to add extra icons into the leftBarButtonItem and rightBarButtonItem for UINavigationController. Here is a picture of one taken from nngroup.com:
In order to do something like this, are they subclassing UINavigationController or adding a category for it? Or are they doing something different? I kind of just wanted the general approach of how to do something like this, but don't need all the code since I'm sure it's a lot of work. Thanks.
You can create a toolbar object and add the buttons to that toolbar and then set the custom view of the right bar button item or left bar button item as this toolbar.
A sample implementation is like this.
UIToolbar * toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,160, 44.5)];
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
UIBarButtonItem * deleteButton = [[UIBarButtonItem alloc]
initWithTitle:#"Delete"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(deletePressed)];
// Button style is the default style
[buttons addObject:deleteButton];
[deleteButton release];
UIBarButtonItem * barItem = [[UIBarButtonItem alloc] initWithTitle:#"Logout" style:UIBarButtonItemStyleBordered
target:[Y3AppDelegate mainApplicationInstance] action:#selector(logout)];
[buttons addObject:barItem];
[barItem release];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:YES];
[buttons release];
// place the toolbar into the navigation bar as Right Button item
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar] autorelease];
[toolbar release];
One thing to note here is that you may have to change the tint color of the toolbar to match with that of the navigation bar. Hope this answer helps you.
You can hide Navigation bar and instead of that show an image of navigation bar and then place buttons on it :))

Adding UIToolbar with two UIBarButtonItem to a UINavigationBar: poor UIToolbar and what about iPhone 4

I'm following the second tip from here. In this tip two UIBarButtonItems are put together in a UIToolbar. Finally, the UIToolbar is added to the UINavigationBar. Now to my problems:
1) A white line is on top of the UIToolbar. If I increase the size of the UIToolbar, the gradient is wrong. I'm using the following size for the UIToolbar:
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 90, 44.01)];
How can I get rid of the white line? See here:
The problem is that there is a white instead of a grey line. If it would be grey, everything would be perfect.
2) What about the difference of the display size of iPhone 3 and iPhone 4? Do I have to check which iPhone is used and then double the size?
Edit:
The buttons are created like in the following example I took from the above mentioned website:
// create a toolbar to have two buttons in the right
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:NULL];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// create a spacer
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[buttons addObject:bi];
[bi release];
// create a standard "refresh" button
bi = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
bi.style = UIBarButtonItemStyleBordered;
[buttons addObject:bi];
[bi release];
// stick the buttons in the toolbar
[tools setItems:buttons animated:NO];
[buttons release];
// and put the toolbar in the nav bar
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
# tc.:
I tried to subclass UIToolbar.
// MyToolbar.h
#import <Foundation/Foundation.h>
#interface MyToolbar : UIToolbar {
}
#end
// MyToolbar.m
#import "MyToolbar.h"
#implementation MyToolbar
- (void)drawRect:(CGRect)rect {
// do nothing
}
- (id)initWithFrame:(CGRect)aRect {
if ((self = [super initWithFrame:aRect])) {
self.opaque = NO;
self.backgroundColor = [UIColor clearColor];
self.clearsContextBeforeDrawing = YES;
}
return self;
}
#end
There's no guarantee that UIToolbar draws seamlessly inside a UINavigationBar; this might be responsible for the white line you're seeing.
You might be able to subclass UIToolbar so that it doesn't draw (i.e. override -drawRect: to not do anything).
1) I can't explain the white line. Curious that it's only above your buttons. How are the buttons created? Also, is there a reason you're setting the height to 44.01, rather than just 44? I'm not positive that the height you set is honored in any case, they may be forced to 44 (someone correct me if I'm wrong).
2) You don't have to do anything for iPhone 4, everything is scaled automatically.
Did you try adding those buttons not inside a container UIView and then adding it as an item but manipulating with UIBarButtonSystemItemFlexibleSpace? I mean adding each of those buttons as an independent item.
An UIToolbar is designed to be used at the bottom of Iphone screen, so if you use somewhere else it will try to make an edge effect on top side. To avoid this the toolbar height should be 2 pixels bigger then Navbar height. Bu this time you will have a different side effect which causes an edge clear on the bottom side of the navbar. (The navbar in any case locates the rightbarbuttonitem as aligned in the middle)

UIBarButtonItem doesn't highlight when tapped if in a Toolbar?

I need two buttons on the left side of a nav bar. The only way I've figured out how to do that is by first putting them into a UIToolbar and then setting the leftBarButtonItem to that.
If I do this it works as normal (you can see it highlight when tapped):
UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething:)];
self.navigationItem.leftBarButtonItem = myBtn;
But if I do it like this, the button action still happens but there is no highlight (no visual feedback that you are tapping the button):
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem* myBtn = [[UIBarButtonItem alloc] initWithTitle:#"Button" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomething:)];
UIBarButtonItem* myBtn2 = [[UIBarButtonItem alloc] initWithTitle:#"Button2" style:UIBarButtonItemStyleBordered target:self action:#selector(doSomethingElse:)];
[buttons addObject:myBtn];
[buttons addObject:myBtn2];
UIToolbar* toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 133, 44)];
[toolbar setItems:buttons animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:toolbar];
Any idea why this causes the buttons to not highlight when touched?
I dont think the UIBarButtonItem object will be highlighted when touched up. Even for default back button in the navigation bar dosent highlight when touched up. It works in that way only . Not sure but you can try using UISegmentedControl with the single segment . It might create the highlighted illusion and would look like barbutton only.
In your selector function (e.g. doSomething), set the tintColor property of the button to your desired highlighted color if pressed, and the default if unpressed. And make sure the highlight color of your button is different than the default color so that you know that it is highlighted.
As per the Apple Docs, the function of showsTouchWhenHighlighted is:
A Boolean value that determines whether tapping the button causes it to glow.
but this is only for UIButton. I don't believe UIBarButtonItem has a highlight option.
Note that UIBarButtonItem inherits from UIBarItem which inherits from NSObject, not UIButton, so you can expect different behavior.