UISegmentedControl's alignment get change when hiding UINavigationItem buttons - iphone

I have an UISegmentedControl in the title view of the navigation bar. Also i have right bar button(plus) and left bar button(Edit) as shown in the below image..
I'm hiding the two bar buttons (here, edit & plus buttons) when the ID segment is clicked. This will be like..
I'm using the following code to hide bar button items.
- (void)segmentClicked {
segment = segmentedControl.selectedSegmentIndex;
self.navigationItem.leftBarButtonItem = leftBarButton; //leftBarButton is an instance of UIBarButtonItem
self.navigationItem.leftBarButtonItem = rightBarButton; //rightBarButton is an instance of UIBarButtonItem
if (segment == 0) {
//Do something for segment 1 click
}
else if (segment == 1) {
//Do something for segment 2 click
}
else {
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.rightBarButtonItem = nil;
}
}
My problem is, when i switch from segment1(Department) or segment2(Name) to segment3(ID), the UISegmentedControl alignment get changed. How can i make it stable?
Thanks in Advance

bar button items align automatically..
if you remove other items the segmented control will shift.
What you need to do is use a 'fixed space' bar item to position your segmented control...
instead of making your leftBarButtonItem nil, change it to a fixed space item of size required to keep the segmented control in place..
if you want to see how it works before implementing, just put a tool bar in a xib and add 'fixed space' and 'flexible space' items..
EDIT:
its pretty straight forward. Just look at the uibarbuttonitem class. create a system bar button item object of type fixed space and set it's width as per your need. and set it as your left bar button:
UIBarButtonItem *item = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
item.width = 50;
self.navigationItem.leftBarButtonItem = item;
[item release];
EDIT 2:
another suggestion - use shorter text for the segments. like use Dept. instead of Department. if your segmented control in title view is short enough it wont be affected by bar buttons.

Related

How can I change the back navigation bar button item text?

I am working on a navigation based application. Until now I am able to do this:
self.navigationItem.title=self.Category;
UIBarButtonItem *btnNewGame = [[UIBarButtonItem alloc] initWithTitle:#"New Game" style:UIBarButtonItemStyleBordered
target:self action:#selector(btnNewGameclicked:)];
self.navigationItem.rightBarButtonItem = btnNewGame;
[btnNewGame release];
and then I can call the method provided by the selector. No problems since here.
What happens now is that I have the left bar button item, the title and the right bar button item.
Problem
The title on the left bar button item is not back, but it appears to be the same as the navigation item's title. How can I change this? Any suggestions?
Thanks.
default behaviour of the left bar button item, is to display the title of the previous view Controller
if you want to change the text on the left bar button item, you need to replace the left (back) button like this
UIBarButtonItem *myBarButtonItem = [[UIBarButtonItem alloc] init];
myBarButtonItem.title = #"Back"; // or whatever text you want
self.navigationItem.backBarButtonItem = myBarButtonItem;
[myBarButtonItem release];
As per your question what i understand , your problem is that the title of leftbarbuttonitem is not Back, this is because you gave the previous screen some title.That's why leftbarbutton item shows the title of navigation-item and not as Back.To remove that you must have erase the title of previous view or do it pro-grammatically as given by Matt.
And why not
self.navigationItem.leftBarButtonItem.title = #"MyTitle";
?
Try this way in the viewDidLoad method:
self.navigationController.navigationBar.topItem.title = #"YourBack";
This worked for me.

How to Hide the UInavigationBar Rightbarbutton item?

I added the info button to the navigation bar using below code:
UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:#selector(showImage:)
forControlEvents:UIControlEventTouchUpInside];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:infoButton];
Now i want to hide this button at some part of the code based on some conditions. But i didn't find any hide property for right bar button item in the navigation bar?
For making hidden, try with assigning nil to your rightBarButtonItem like below.
self.navigationItem.rightBarButtonItem = nil ;
Best option is to use buttonItem.enabled = NO to indicate the functionality isn't available at the moment. That should, in most cases, be the right behavior.
However if you intend to make it disappear, the best way would be to store a reference to the bar button. Set the rightBarButtonItem to nil when you want it to disappear and set it to the stored reference when you want it to be displayed.
If you have multiple UIBarButtonItems and you just want to remove one, you can do this:
NSMutableArray *barButtonItems = [self.toobbar.items mutableCopy];
[barButtonItems removeObject:self.buttonToRemove];
[self.toolbar setItems:[barButtonItems copy] animated:NO];
If you just want to 'hide' it visually:
Swift 3:
self.navigationItem.rightBarButtonItem?.tintColor = UIColor.clear
self.navigationItem.rightBarButtonItem?.isEnabled = false

Problems adding a UIBarButtonItem programmatically or editing one inserted via IB

I'm working on project that uses a tab bar at the bottom to flip through 5 sections of my app. One of these sections loads a map view which then spawns a web view. This was done with Round Rect Buttons at first, but they got in the way of the views, so I decided to switch to a navigation bar. In Interface Builder I inserted a navigation item and within it two Bar Button Items (left and right side). I've gotten the functionality for both button to work by control dragging from file's owner to fire the method associated with each button. This works fine and launches my web view, but what I would like to be able to do and have yet to figure out how to so far, is changing one of the buttons to a "done" or "back" button once in the web view. I've tried creating both buttons programmatically but neither would appear. This is the code I tried using to accomplish this...
UIBarButtonItem *updatePosition = [[UIBarButtonItem alloc] initWithTitle:#"Update Position"
style:UIBarButtonItemStylePlain target:self action:#selector(findMe)];
self.navigationItem.leftBarButtonItem = updatePosition;
[updatePosition release];
When I run the app, the button does not show up on the navigation item I inserted using Xcode. The only way I can get the buttons to appear is by inserting them with Interface Builder as I previously mentioned. But then I can not change the left button to a "back" or "done" button once I've brought up the new view. I tried running this code to change the style of the button on the UIBarButtonItem I inserted using IB with this code
self.navigationItem.leftBarButtonItem.style = UIBarButtonItemStyleDone;
But to no avail.
Here are some Utility methods to help you along the way:
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withImage:(NSString *)imageName {
UIImage *buttonImage = [UIImage imageNamed:imageName];
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithImage:buttonImage
style:UIBarButtonItemStyleBordered
target:sender
action:action] autorelease];
return buttonItem;
}
+ (UIBarButtonItem *)setRightBarButtonItem:(SEL)action target:(id)sender withBarButtonSystemItem:(UIBarButtonSystemItem)systemItem {
UIBarButtonItem *buttonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:systemItem
target:sender
action:action] autorelease];
return buttonItem;
}
Usage:
// Done button.
self.navigationItem.rightBarButtonItem = [Utility setRightBarButtonItem:#selector(doneTapped:) target:self withBarButtonSystemItem:UIBarButtonSystemItemDone];

adding icon between back button & title of navigation bar

I want to put an icon by side of the title of my navigation bar. I'd prefer not to implement it as a custom titleView, because then i'll need to create a custom titleView for each controller i put on the stack (and I have pretty deep nesting). I'm adding currently as an UIImageView to a navigationBar. My problem is to calculate exactly this icon's horizontal position. It depends on the width of the back button, which has each time another title. How do I calculate this back button frame? Googling on it seems doesn't bring any reasonable results.
Thanks in advance
You could calculate the size of a label, with
your text in it and experiment with what the button will add on..
CGSize s = [label sizeWithFont:_font];
I ran into this same issue. My solution was to add the icon as one of the leftBarButtonItems, after a flexible space. This pushes it up against the title.
UIBarButtonItem *space = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil] autorelease];
UIView *icon = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"titleIcon"]] autorelease];
UIBarButtonItem *iconItem = [[[UIBarButtonItem alloc] initWithCustomView:icon] autorelease];
NSArray *items = [NSArray arrayWithObjects: space, iconItem, nil];
self.navigationItem.leftBarButtonItems = items;
self.navigationItem.leftItemsSupplementBackButton = YES;
It's not quite the same as creating a custom titleView, however, in two ways: (1) the title text remains centered if possible, with the icon to its left, rather than centering the title and icon together; and (2) when you push a new view onto the stack, the icon does not slide to the left with the title; instead it fades out, like the other bar button items.
So, depending on your requirements, this solution may or may not work for you. But at least it avoids trying to match the standard title text attributes in a custom title view. (As far as I can discover, there is no automatic way to do that.)

Adding button to left of UISearchBar

I am tearing my hair out on this one. My client wants to add a button to the left of a search bar like the example below:
(source: erik.co.uk)
But I just can't figure out how to do it. Apple don't seem to provide any documented method for adding custom buttons to a UISearchBar, let alone to the left of the search bar.
I've tried hacking around in Interface Builder adding a UIToolbar with a button in it to the left but I cannot find any combination of styles where the two line up properly to give the impression that they are one. There is always what looks like one pixel difference in the vertical alignment as you can see from the picture below:
(source: erik.co.uk)
I've searched around and just can't find the answer, but as we can see from the screenshot it must be possible!
Thank you in advance for your help.
Erik
Use a navigation bar instead of a toolbar. Set the search bar to the navigation bar's title view.
In Interface Builder:
Result:
You can replace the Bookmark image instead, and adjust its offset if necessary.
For example:
[self.searchDisplayController.searchBar setImage:[UIImage imageNamed:#"plus2"] forSearchBarIcon:UISearchBarIconBookmark state:UIControlStateNormal];
[self.searchDisplayController.searchBar setPositionAdjustment:UIOffsetMake(-10, 0) forSearchBarIcon:UISearchBarIconBookmark];
Handle the button event in the delegate method:
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar
This is how it looks:
The first solution is to use UINavigationBar instead of UIToolbar, as KennyTM noticed. But you may not be satisfied with Navigation bar, like in my case, when I need to use 3 buttons (Navigation bar is allow to use only 2 buttons) - see the left picture. This is how I did it:
Use Toolbar with 3 buttons and Flexible Space Bar Button Item in the place where search bar should be placed.
Put search bar on (not in) the toolbar. To do so in Interface Builder, do not drag & drop the search bar on the toolbar. Instead, put it somewhere nearby and then move it to place using the arrow keys on the keyboard (or by changing X & Y position in Interface Builder).
Search bar left black line under it (see the right picture). To hide it I put one additional view with the height 1px and a white background over it.
It looks a bit dirty for me, so if you have a better solution, let me know.
The easiest solution is to add your SearchBar in TOP of your Toolbar, (not in), I give you the best solution I use in my company eBuildy:
UIBarButtonItem *mySettingsButton = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(refresh)];
UIBarButtonItem *mySpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *myRefreshButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh)];
UIToolbar *myTopToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0,0,320,40)];
UISearchBar *mySearchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(70,1,220,40)];
[myTopToolbar setItems:[NSArray arrayWithObjects:mySettingsButton,mySpacer,myRefreshButton, nil] animated:NO];
[self.view addSubview:myTopToolbar];
[self.view addSubview:mySearchBar];
answering an old question here but i was struggling with this one myself recently and found some shortcomings with the other answers for the situation i was trying to address. here's what i did in a subclass of UISearchBar:
first add a UIButton property (here "selectButton"). then override the initWithFrame method and do something similar to the following:
-(id)initWithFrame:(CGRect)frame{
if (self = [super initWithFrame:frame])
{
self.selectButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
self.selectButton.contentEdgeInsets = (UIEdgeInsets){.left=4,.right=4};
[self.selectButton addTarget:self action:#selector(pressedButton:) forControlEvents:UIControlEventTouchUpInside];
self.selectButton.titleLabel.numberOfLines = 1;
self.selectButton.titleLabel.adjustsFontSizeToFitWidth = YES;
self.selectButton.titleLabel.lineBreakMode = UILineBreakModeClip;
[self addSubview:self.selectButton];
[self.selectButton setFrame:CGRectMake(5, 6, 60, 31)];
}
return self;
}
Now you want to override the layout subviews method to resize the searchbar to the appropriate width, depending on whether or not the cancel button is showing. That should look something like this:
-(void)layoutSubviews
{
[super layoutSubviews];
float cancelButtonWidth = 65.0;
UITextField *searchField = [self.subviews objectAtIndex:1];
if (self.showsCancelButton == YES)
[searchField setFrame:CGRectMake(70, 6, self.frame.size.width - 70 - cancelButtonWidth, 31)];
else
[searchField setFrame:CGRectMake(70, 6, self.frame.size.width - 70, 31)];
}
Note that in the above method I added a constant for the cancelButtonWidth. I tried adding code to get the width from [self cancelButton] but that seems only accessible at runtime and doesn't allow the project to compile. In any case this should be a good start for what you need
If you want a custom button on the right, taking place of the Cancel button, just use this code (valid for iOS 9 and up):
[self.searchBar setShowsCancelButton:YES];
[[UIBarButtonItem appearanceWhenContainedIn:[self.searchBar class], nil] setTitle:#""];
[[UIBarButtonItem appearanceWhenContainedIn:[self.searchBar class], nil] setImage:[UIImage imageNamed:#"search"]];