How to add more than one UIBarButtonItem to rightBarButtonItem in ios7? - objective-c

I have an existing code which was working fine in ios6. but in ios7 left most item("refresh button") is not showing align to other two UIBarButtonItem . its showing little down. here is the code for iOS6. what changes do i need to make this working in iOS7.
// create an array for the buttons
NSMutableArray* buttons = [[NSMutableArray alloc] initWithCapacity:3];
// create a standard save button
UIBarButtonItem* refreshButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh
target:self
action:#selector(refreshButtonClicked:)];
refreshButton.style=UIBarButtonItemStyleBordered;
//self.navigationItem.rightBarButtonItem = refreshButton;
[buttons addObject:refreshButton];
[refreshButton release];
// create a spacer between the buttons
UIBarButtonItem *spacer = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:nil];
[buttons addObject:spacer];
[spacer release];
// create a standard delete button with the trash icon
UIButton *button = [UIButton buttonWithType:UIButtonTypeInfoDark];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button addTarget:self action:#selector(InfoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
infoItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[buttons addObject:infoItem];
// put the buttons in the toolbar and release them
[toolbar setItems:buttons animated:NO];
[buttons release];
// place the toolbar into the navigation bar
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]
initWithCustomView:toolbar]autorelease];
Thanks

Try:
self.navigationItem.rightBarButtonItems = buttons;

Related

How do I correctly set the tint colour of a UINavigationBar in iOS7?

I'm using the last two lines of the code below to change the tint colour of my navigation bar so that my UIBarButtonItems display as black instead of the default blue. This code works in another controller but not in this controller.
When I navigate forward to that controller then navigate back to the controller in question then the UIBarButtonItems are black as needed. However when I first load the app and this view is loaded they are blue.
How do I correctly change the tint colour of my navigation bar so my UIBarButtonItem's display as black?
The actual images I've set for the buttons are black.
Code in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
// Logo button
UIButton *logoButton = [[UIButton alloc] init ];
[logoButton setImage:[UIImage imageNamed:#"va_logohme.png"] forState:UIControlStateNormal];
[logoButton setAdjustsImageWhenHighlighted:NO]; ;
[logoButton setFrame:CGRectMake(0, 0, 320, 40)];
[logoButton addTarget:self action:#selector(logoButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
[[self navigationItem] setTitleView:logoButton];
// Additional UIBarButtonItem's
UIBarButtonItem *menuButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"hamburger_for_more.png"] style:UIBarButtonItemStylePlain target:self action:#selector(menuButtonTap)];
UIBarButtonItem *searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"magnify_glass.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTapped)];
[[self navigationItem] setLeftBarButtonItems:#[menuButton, searchButton]];
UIBarButtonItem *shoppingCartButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"shopping_bag.png"] style:UIBarButtonItemStylePlain target:self action:#selector(shoppingCartButtonTapped)];
UIBarButtonItem *addFavouriteButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"add_fav_heart.png"] style:UIBarButtonItemStylePlain target:self action:#selector(favouritesPageButtonTapped:)];
NSMutableArray *buttons = [[NSMutableArray alloc] init];
for (UIControl *btn in self.navigationController.navigationBar.subviews) {
if ([btn isKindOfClass:[UIControl class]]) {
[buttons addObject:btn];
}
}
// Basket container
_bagContainer = [[UIView alloc] initWithFrame:CGRectMake(277, 16, 21, 21)];
[[[self navigationController] navigationBar] addSubview:_bagContainer];
UILabel *bagCount = [[UILabel alloc] init];
[bagCount setText: [NSString stringWithFormat:#"%i", [Bag totalItems:[self managedObjectContext]]]];
[bagCount setFont:[UIFont systemFontOfSize:14]];
[bagCount sizeToFit];
[_bagContainer addSubview:bagCount];
bagCount.center = [_bagContainer convertPoint:_bagContainer.center fromView:_bagContainer.superview];
[_bagContainer setUserInteractionEnabled:NO];
[[self navigationItem] setRightBarButtonItems:#[shoppingCartButton, addFavouriteButton] animated: YES];
// Change colour of nav bar tint
[[[self navigationController] navigationBar] setBarTintColor:[UIColor whiteColor]];
[[[self navigationController] navigationBar] setTranslucent:NO];
}
If the images are template images, then set their tintColor to the desired color. If they are normal images, then make sure to render them as normal images so that they show up in their own color and not as tinted template images.

How to change leftBarbuttonItem title?

I want to change leftBarButton title. I've tried this
UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonItemStyleBordered
target:nil
action:nil];
[[self navigationItem] setLeftBarButtonItem:newBackButton];
but it brings changes like
But I want it like
How it can be possible?
It may helpful for you
UIBarButtonItem *backBarButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleDone target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = backBarButton;
From the view controller (ViewController1) that is pushing the next view controller (ViewController2), you need to set the backBarButtonItem.
#implementation ViewController1
- (void)viewDidLoad
{
[super viewDidLoad];
self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"New Title" style:UIBarButtonItemStylePlain target:nil action:nil];
}
It's totally normal that you don't see any arrow in your back button. That's because you're creating a new bar button that you set its title and its style. But the style you've set for your bar button item won't give you what you wanted.
I suppose that you're using UINavigationController to navigate. If it's the case, you need to get a reference to navigationBar that UINavigationController displays on the screen. Then, you'll be able to change its color via tint color property. So,
[self.navigationController.navigationBar setTintColor:[UIColor redColor]];
would do what you want to achieve. Here is the image i obtained using the code i mentioned above.
If you're not using UINavigationController, the simplest solution is to provide an image for your back button which has already an arrow.
You should set backBarButtonItem instead of leftBarButtonItem. And remember to set it not in current view controller (where you wish it to be displayed) but in parent one (to which this button will return you).
Use this Code and
UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *backBtnImage = [UIImage imageNamed:#"BackBtn.png"] ;
[backBtn setBackgroundImage:backBtnImage forState:UIControlStateNormal];
[backBtn addTarget:self action:#selector(goback) forControlEvents:UIControlEventTouchUpInside];
backBtn.frame = CGRectMake(0, 0, 54, 30);
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithCustomView:backBtn] ;
self.navigationItem.leftBarButtonItem = backButton;
and for going to back use this method .
-(void)goback
{
[self.navigationController popViewControllerAnimated:YES];
}
This worked for me
UIImage *image = [[UIImage imageNamed:#"btn_back.png"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = button;
- (IBAction)backAction:(id)sender
{
MainMenuViewController *mainMenuViewController = [[MainMenuViewController alloc] init];
[self.navigationController pushViewController:mainMenuViewController animated:YES];
}

UIPickerView toolbar buttons not working

I'm trying to add a toolbar to a UIPicker.
I've seen that the right way to go is this way:
UIToolbar* toolbarWeight= [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,320,44)];
UIBarButtonItem *barButtonDone1 = [[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextField:)];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];
weightPickerView = [[UIPickerView alloc]init];
[weightPickerView addSubview:toolbarWeight];
While:
-(IBAction)gotoNextField:(id)sender{
NSLog(#"Works");
}
The picker works just fine but the button doesn't work.
After doing some research, I've tried this approach:
(I suspected that the problem was related to the UIBarButtonItem action)
UIButton* myBtn = [[UIButton alloc]init];
myBtn.frame = CGRectMake(0,0,50,24);
[myBtn addTarget:self action:#selector(gotoNextField:) forControlEvents:UIControlEventTouchUpInside];
[myBtn setTitle:#"Next!" forState:UIControlStateNormal];
[myBtn setBackgroundColor:[UIColor orangeColor]];
UIBarButtonItem *barButtonNext1 = [[UIBarButtonItem alloc] initWithCustomView:myBtn];
[toolbarWeight setItems:[NSArray arrayWithObject:barButtonNext1] animated:YES];
Doesn't work either. The button appears but doesn't get selected/respond to touchUpInside.
Here an example for adding a UIPickerView including a Toolbar - works with iOS 7
Make sure to implement the interfaces UIPickerViewDataSource, UIPickerViewDelegate and also implement the #selector methods
pickerView = [[UIPickerView alloc] init];
[pickerView setDataSource: self];
[pickerView setDelegate: self];
pickerView.showsSelectionIndicator = YES;
UIToolbar *toolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(itemWasSelected:)];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(logoutData)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:#"Clear" style:UIBarButtonItemStyleBordered target:self action:#selector(clearData)];
[toolBar setItems:[NSArray arrayWithObjects:cancelButton, clearButton, flexible, doneButton, nil]];
pickerParentView = [[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 260)];
[pickerParentView addSubview:pickerView];
[pickerParentView addSubview:toolBar];
[self.view addSubview:pickerParentView];
As adding a toolbar to the pickerView, the toolbar is added behind the picker preventing the buttons from responding to touch events.
Encapsulating both the picker and the toolbar in a view, and making it an inputView (to make both the picker and the toolbar popup together) provides the desired solution.
weightPickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 44, 320, 216)];
weightPickerView.tag = 13;
UIView* genericWeightView = [[UIView alloc]initWithFrame:CGRectMake(0, 219, 320, 266)];
[genericWeightView addSubview:toolbarWeight];
[genericWeightView addSubview:weightPickerView];
and then:
[weight_txtField setInputView:genericWeightView];

Odd bug with UISplitView - Viewport scales to iPhone size?

I'm working with Objective-C for the first time, and have an odd bug in an application that was given to me. The following pictures demonstrate the problem I'm having:
This screen displays when the user rotates the iPad. This is normal. The control at the top (<) allows the user to hide the subview on the left side. Upon hiding the subview, the dark blue portion should expand to fill the screen. Prior to my working with the application, this worked. Now, this is happening:
I haven't found anything to explain why, exactly, this is happening. Any ideas?
- (void)viewDidLoad {
[super viewDidLoad];
[self addGestureRecognizers];
// initialize the "<" button...
toggleButton = [[UIBarButtonItem alloc] initWithTitle:[PanoUtils leftIndicator] style:UIBarButtonItemStylePlain target:self action:#selector(toggleScreen)];
// the annotate button...
annotateButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Pencil.png"] style:UIBarButtonItemStylePlain target:self action:#selector(annotateTapped)];
// the done button for annotations...
doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneAnnotateTapped)];
// a flexible spacer...
nameFieldSpacerButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
// the accelerometer button...
alignButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"gyro.png"] style:UIBarButtonItemStylePlain target:self action:#selector(align:)];
// and the search button.
searchButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"Search.png"] style:UIBarButtonItemStylePlain target:self action:#selector(searchButtonTouched:)];
// customize the align button after the user selects it.
UIImage *buttonImage = [UIImage imageNamed:#"gyroSelected.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:buttonImage forState:UIControlStateNormal];
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height);
[button addTarget:self action:#selector(align:) forControlEvents:UIControlEventTouchUpInside];
alignSelButton = [[UIBarButtonItem alloc] initWithCustomView:button];
NSMutableArray *itemsArray = [toolBar.items mutableCopy];
[itemsArray addObject:searchButton];
[itemsArray addObject:annotateButton];
[itemsArray addObject:alignButton];
[toolBar setItems:itemsArray animated:NO];
[itemsArray release];
[self adjustToggleButton];
}

ios) Object-c. how to customize a button on the navigation bar

I'm developing a navigation-base application with a custom navigation bar. I succeeded to have a navigation bar with an image and a button. However I have no idea how to make the custom button..:(
This is the code that adds the "main"button on the navigation bar.(in the view controller, initWithNibName method)
if (self) {
// Custom initialization.
//set navigation id to I to inform that that page is inforview
navID = #"I";
//adds the button on the navigation bar
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Main" style:UIBarButtonItemStyleBordered target:self action:#selector(goBack:)];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
[button release];
[navID release];
}
Thank you
I created a custom button and applied it to the UIbarbuttonitem.
There is no error but nothing is shown on the navigation bar:(
This is my code-
//create a custom button
UIImage *image = [UIImage imageNamed:#"TESTButton.png"];
UIButton *myCustomButton = [UIButton buttonWithType:UIButtonTypeCustom];
myCustomButton.bounds = CGRectMake( 0, 0, image.size.width, image.size.height );
[myCustomButton setImage:image forState:UIControlStateNormal];
[myCustomButton addTarget:nil action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithCustomView:myCustomButton];
self.navigationItem.leftBarButtonItem = button;
self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
Anybody who can fix my code? :)
The easiest way is to use:
UIButton *yourCustomButton = ....
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:yourCustomButton];
self.navigationItem.leftBarButtonItem = barButtonItem;
[barButtonItem release];