How to make bar button items have 2 lines of text? - ios

I try to follow this one, but it doesn't work.
How can we put two line in UIBarButtonItem in Navigation Bar
I don't know why the button view I created and assigned to the bar button item cannot display at all?
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
button1.titleLabel.numberOfLines = 0;
[button1 setTitle:NSLocalizedString(#"REQUEST\nEQUIPMENT", nil) forState:UIControlStateNormal];
[button1 sizeToFit];
self.barButton = [[UIBarButtonItem alloc] initWithCustomView:button1];
Or is there any better way to have more than 1 line of text in the UIBarButtonItem?

The problem is that you are making a bar button item but you are never putting it into the interface. Setting self.barButtonItem doesn't put the bar button item into the interface; it just retains the bar button item. This has nothing at all to do with the "two-line" question. You cannot make any bar button item appear by doing what you're doing.
A bar button item appears when, say, you are a view controller in a navigation interface and you set self.navigationItem.rightBarButtonItem to your bar button item. But you are not doing that.

Related

iOS: possible to superimpose barbuttonitem on other barbuttonitem and swap them

I have a screen that displays some info in a textview and I would like the user to be able to edit it.
Right now, I have an edit button on the right side of the navigation bar that I create in code as follows:
UIBarButtonItem *editButton = [[UIBarButtonItem alloc] initWithTitle:#"Edit" style:UIBarButtonItemStylePlain target:self action:#selector(gotoEdit)];
self.navigationItem.rightBarButtonItem = editButton;
For editing, I could launch a new view controller but it would be cleaner, I think, to just use the textview.editable property to make the text view editable.
However, I would then need to change the title and function of the uibarbuttonitem from edit to save.
You apparently cannot change the title of a system edit button and I'd just as soon not create a custom bar button item although this may ultimately prove necessary.
It is possible to hide bar button items by making their color clear and disabling interaction. Therefore, I had the idea of putting two in the same place and hiding and showing them accordingly.
Hence my question. Is it possible to put two in the same place?
Thanks for any ideas on this.
If you are using storyboards...you can drag and drop a UIButton to the rightBarButtonItem. Make this button a property and set the initial title to "Edit".
In the Action:
- (IBAction) editButton (id){
if(!isEditing){
// Prep for editing
[self.editButton setTitle:"Save" forState:UIControlStateNormal];
isEditing = true;
}else{
// Prep for save
[self.editButton setTitle:"Edit" forState:UIControlStateNormal];
isEditing = false;
}
}

How can I set two buttons on the right side of an UINavigationBar?

I have made a standard Master-Detail application (with the Xcode template). I modified it, and now when I click one cell of the first table view the app shows another table view (which is the detail because is filtered by the clicked cell value).
In a standard table view I would have this situation on the UINavigationBar:
Edit Table title + (for adding new items)
I need the default back button of UINavigationBar too. Is it possible and allowed to do this? Graphically it would be like this:
< Back Table title Edit +
Do you know any other layout to display 3 button (back - edit - add) plus title all in the top bar?
Sorry for not posting images, Thanks in advance.
P.S. I'd like to know if it is possible to do the things I'm asking with built-in back button and edit button (I mean those that the system puts automatically to the navigation bar).
you need to setup array of right bar items to set up bar buttons right side...
Here is the code of 2 custom button to setup bar button items right side...you can use default button instead of this
UIButton *btnabout = [UIButton buttonWithType:UIButtonTypeCustom];
btnabout.frame = CGRectMake(0,8,30,30);
[btnabout setBackgroundImage:[UIImage imageNamed:#"about.png"] forState:UIControlStateNormal];
[btnabout addTarget:self action:#selector(callselector) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *about = [[UIBarButtonItem alloc] initWithCustomView:btnabout];
about.tintColor = [UIColor whiteColor];
UIButton *btnsetting = [UIButton buttonWithType:UIButtonTypeCustom];
btnsetting.frame = CGRectMake(0,8,30,30);
[btnsetting setBackgroundImage:[UIImage imageNamed:#"setting"] forState:UIControlStateNormal];
[btnsetting addTarget:self action:#selector(SettingData) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *setting = [[UIBarButtonItem alloc] initWithCustomView:btnsetting];
setting.tintColor = [UIColor whiteColor];
self.navigationItem.rightBarButtonItems =[NSArray arrayWithObjects:setting, about, nil];
// --- or if you want left side ----
// self.navigationItem.leftBarButtonItems =[NSArray arrayWithObjects:setting, about, nil];
[self.navigationItem setRightBarButtonItems:#[/*array of UIBarButtonItem*/] animated:NO]
Apple doc
to someone stuck with this problem in swift, here is the correct answer :
let barButton_array: [UIBarButtonItem] = [Button1, Button2]
navigationItem.setRightBarButtonItems(barButton_array, animated: false)

Anyone know how to remove unwanted animation from navigation bar? Screenshots

I have a view controller which proceeds via a button to a table view controller.
In the view controller, the navigation bar is totally translucent (As you can see in the screen shot below). On the table view controller, the navigation bar is set to white.
My problem is, when I press 'back' in the table view and return to the view controller, the white navigation bar is carried over for a moment (see top image) before disappearing in an ugly animation.
Extra Navigation bar space:
How I want it to always look:
I have tried pretty much everything I can think of, all my code relating to the navigation bars translucency is in viewDidAppear, so why is this happening!?
Someone please tell me what I'm doing wrong! This is making me crazy!
In the tableviewcontroller set:
- (void)viewWillDisappear:(BOOL)animated {
// put the code for the uinavigation bar styling here.
}
You can do some thing like this have a custom back button below is the code
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(2, 1, 29, 29);
[backButton setBackgroundImage:[UIImage imageNamed:#"back_button"] forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
backButton.adjustsImageWhenHighlighted = NO
item.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
where item is instance of UINavigationItem
and in selector of back button
-(void)backButtonClicked:(id)sender {
[self.navigationBar popNavigationItemAnimated:NO];
}
by doing this the navigation bar will pop the item but without animation.

navcontroller - Setting up Navigation Item as image

I would like to setup the navigation controller bar button item to be an image.
I have read the following - Add image to a navigationItem's title
I understand how to setup this as an image. I am actually looking to set this up as the setting cog, within the bar button item (like a cog icon). I have seen this before in other apps, I would like to know if there is a default style to initialise for this or normally if this is achieved by the image route as above.
If this is above, does anyone know where this (kind of) default image styled icon is available, or would I need to make one?
Have you tried using the initWithCustomView option of UIBarButtonItem? A sample code will look like the below line.
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"settings.png"]]];
Another way to do this is,
UIBarButtonItem *aButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"settings.png"] style:UIBarButtonItemStyleBordered target:self action:#selector(buttonTapped)];
If you use storyboard, you can directly:
Drag a Button in the navigation bar instead of a Bar Button Item. It will automatically create a Button Bar Item for you with a Button inside it.
Enter a title for the Button Bar Item
Set an image for the Button with a Custom type.
It should works.

Back, Edit and Add buttons in navigation bar of TableView with Storyboard on iOS

I'm facing some problems in implementing a tableview, with "Back", "Edit" and "Add" buttons on the navigation bar.
The tableview is reached by clicking on a row of another tableview, so the "Back" button is added automatically.
With the storyboard I've added the "Add" button to the navigation bar.
With code I've added the "Edit" button (I used code, since if I add the button with the storyboard, I don't know how to reproduce the "Edit" standard behavior...):
self.navigationItem.leftBarButtonItem = self.editButtonItem;
The problem is that, in this way, the "Edit" button hides the "Back" button on the navigation bar.
At this point, I've two questions:
Is it possible with storyboard to add a third button on the navigation bar?
In case I've to do this programmatically, I know that I can do this as follows:
UIButton *button = [UIButton buttonWithType: UIButtonTypeRoundedRect];
[button setFrame:CGRectMake(width-90,6,50,30)];
[button setTitle:#"Edit" forState:UIControlStateNormal];
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
[self.navigationController.navigationBar addSubview:button];
But how can I implement via code the standard behavior of the "Edit" button? I mean, I click "Edit" and the button becomes "Done" and the rows become deletable...
Thanks in advance,
yassa
Incase anyone else should happen to stumble onto this question as well the solution is pretty easy. UINavigationItem has a property for rightItems wich is just an array of UIBarButtonItems. Put both an add button and an edit button into an array and assign it to rightItems and your done :-) And here is an example code snippet:
UITableViewController *table = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(insertNewObject:)];
NSArray *barButtons = [NSArray arrayWithObjects:table.editButtonItem, addButton, nil];
table.navigationItem.rightBarButtonItems = barButtons;
First, the Apple docs say 'you do not add subviews to a navigation bar directly'. Don't know if this is enough to get the app bounced from the store, but it's not considered "proper".
Second, you can add more than three buttons to a UINavigationItem in iOS 5 but not in iOS 4 or earlier.
Finally, I'd leave the edit button top right and back top left. That's where people expect them. If I wanted an add button (and are on iOS 5), I'd place it next to the edit button.
Sorry; no help on storyboards. Don't know anything about them.

Resources