I want to insert one button and one label in my NavigationBar in iOS.
I have tried with UISegmentedControl and it works completely fine with one control!
Now the problem is I want to add multiple controls as i have said before How can I?
Look at my code
UIView *v;
[v insertSubview:listingsLabel atIndex:0];
[v insertSubview:shareBtn atIndex:1];
[v setFrame:[self.navigationController.toolbar bounds]];
self.navigationItem.titleView = v;
v.frame = CGRectMake(0, 0, 200, 29);
and it gives me error of EXC_BAD_ACCESS
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 100, 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
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray array]];
[segmentedControl insertSegmentWithTitle:#"All" atIndex:0 animated:NO];
[segmentedControl insertSegmentWithTitle:#"Related" atIndex:1 animated:NO];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl addTarget:self action:#selector(segmentedAction:) forControlEvents:UIControlEventValueChanged];
// create a standard "add" button
UIBarButtonItem* bi = [[UIBarButtonItem alloc] initWithCustomView: segmentedControl];
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:UIBarButtonSystemItemSave target:self action:#selector(save:)];
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];
Because you don't init the UIView the right way, it crashes because the iPhone doesn't know what to do with
[v insertSubview:listingsLabel atIndex:0];
This is because v isn't an object yet. So change
UIView *v;
to
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 29)];
And release it here again (if not using arc)
self.navigationItem.titleView = v;
[v release];
The navigation bar on iPhone only supports only a left and right bar button item, and a title view. Only the (larger) navigation bar on iPad allows an arbitrary number of buttons.
If you're using a navigation bar without a navigation controller, I suppose you could just plop whatever you want onto it as subviews.
UIButton *btnBack = [[UIButton alloc]initWithFrame:CGRectMake(5,5,60,32)];
[btnBack setBackgroundImage:[UIImage imageNamed:#"back_btn.png"] forState:UIControlStateNormal];
btnBack.backgroundColor = [UIColor clearColor];
[btnBack addTarget:self action:#selector(eventBack:) forControlEvents:UIControlEventTouchUpInside];
UILabel *lbl = [[UILabel alloc]initWithFrame:CGRectMake(12, 2, 60,25)];
[lbl setBackgroundColor:[UIColor clearColor]];
lbl.font = [UIFont fontWithName:#"Helvetica" size:12];
lbl.font = [UIFont boldSystemFontOfSize:12];
lbl.textColor = [UIColor whiteColor];
lbl.text =#" Back";
[btnBack addSubview:lbl];
[lbl release];
UIBarButtonItem *backBarBtn = [[UIBarButtonItem alloc] initWithCustomView:btnBack];
self.navigationItem.leftBarButtonItem = backBarBtn;
[btnBack release];
[backBarBtn release];
UILabel *lblTitle = [[UILabel alloc] initWithFrame:CGRectMake(110, 0, 170, 40)];
lblTitle.text = #"ABC";
lblTitle.backgroundColor = [UIColor clearColor];
lblTitle.textColor = [UIColor whiteColor];
lblTitle.textAlignment = UITextAlignmentCenter;
lblTitle.font = [UIFont fontWithName:#"Helvetica" size:17];
lblTitle.font = [UIFont boldSystemFontOfSize:17];
self.navigationItem.titleView = lblTitle;
[lblTitle release];
Related
I wanted to make my right bar button contain 2 buttons. So I did that by using a UIToolbar. But the problem is that the 2 buttons sit apart from each other, while the affect I would like to achieve is too have them sitting flush against each other.
Here is an image of how they look now
Here is the code I use to achieve the buttons so far
UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 2.0f, 120.0f, 40.01f)]; // 44.01 shifts it up 1px for some reason
tools.clearsContextBeforeDrawing = NO;
tools.clipsToBounds = YES;
tools.tintColor = [UIColor blueColor];
tools.barStyle = -1;// -1; // clear background
NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2];
UIButton * upButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
upButton.frame = CGRectMake(0, 07, 46, 30);
upButton.titleLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:16];
[upButton setTitle:#"" forState:UIControlStateNormal];
upButton.backgroundColor = [UIColor clearColor];
[upButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[upButton addTarget:self action:#selector(pageDown) forControlEvents:UIControlEventTouchUpInside];
[upButton setBackgroundImage:[UIImage imageNamed:#"page_up.png"] forState:UIControlStateNormal];
[upButton setBackgroundImage:[UIImage imageNamed:#"page_up_action.png"] forState:UIControlStateSelected];
UIBarButtonItem *toggleSegmentedControlBarItemOne = [[UIBarButtonItem alloc] initWithCustomView:upButton];
[buttons addObject:toggleSegmentedControlBarItemOne];
UIButton * downButton = [[UIButton buttonWithType:UIButtonTypeCustom] retain];
downButton.frame = CGRectMake(0, 07, 46, 30);
downButton.titleLabel.font = [UIFont fontWithName:#"Arial-BoldMT" size:16];
[downButton setTitle:#"" forState:UIControlStateNormal];
downButton.backgroundColor = [UIColor clearColor];
[downButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal ];
[downButton addTarget:self action:#selector(pageUp) forControlEvents:UIControlEventTouchUpInside];
[downButton setBackgroundImage:[UIImage imageNamed:#"page_down.png"] forState:UIControlStateNormal];
[downButton setBackgroundImage:[UIImage imageNamed:#"page_down_action.png"] forState:UIControlStateSelected];
UIBarButtonItem *toggleSegmentedControlBarItemTwo = [[UIBarButtonItem alloc] initWithCustomView:downButton];
[buttons addObject:toggleSegmentedControlBarItemTwo];
[tools setItems:buttons animated:NO];
[buttons release];
UIBarButtonItem *twoButtons = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
self.navigationItem.rightBarButtonItem = twoButtons;
[twoButtons release];
Can anybody please advise me how to get those two buttons to sit beside each other without any gap?
Many Thanks,
-Code
You can add UISegmentedControl as customView to your navigationItem and set its momentary property to true, that way you will get exactly what you want.
Here is sample code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.title = #"Test";
UISegmentedControl *segControl = [[UISegmentedControl alloc] initWithFrame:CGRectMake(0, 0, 90, 30)];
segControl.momentary = YES;
segControl.segmentedControlStyle = UISegmentedControlStyleBar;
segControl.tintColor = [UIColor darkGrayColor];
[segControl insertSegmentWithImage:[UIImage imageNamed:#"up.png"] atIndex:0 animated:NO];
[segControl insertSegmentWithImage:[UIImage imageNamed:#"down.png"] atIndex:1 animated:NO];
[segControl addTarget:self action:#selector(segButtonDown:) forControlEvents:UIControlEventValueChanged];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:segControl];
}
- (void)segButtonDown:(id)sender {
UISegmentedControl *segControl = (UISegmentedControl *)sender;
switch (segControl.selectedSegmentIndex) {
case 0:
NSLog(#"Up");
break;
case 1:
NSLog(#"Down");
default:
break;
}
}
And here is image how it looks like.
And here you can see events in console.
2012-11-25 16:03:47.805 test2[13886:c07] Up
2012-11-25 16:03:48.367 test2[13886:c07] Down
2012-11-25 16:03:48.930 test2[13886:c07] Up
2012-11-25 16:03:49.538 test2[13886:c07] Down
take a look at this tutorial, maybe it helps :
Xcode: Multiple Buttons in Nav Bar
Add a no space space between two buttons.
two do this.
UIBarButtonItem *noSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
noSpace.width = -10.0;
insert this noSpace objet to you'r array (buttons in your case). between two objects of UIBarButtonItem.
it'l be ok..
I have a UINavigationBar and have the leftBarButtonItem and rightBarButtonItem set. The leftbar is set as:
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton setImage:[UIImage imageNamed:#"back.png"] forState:UIControlStateNormal];
UIBarButtonItem *myButton = [[[UIBarButtonItem alloc] initWithCustomView:backButton] autorelease];
self.navigationItem.leftBarButtonItem = myButton;
and the rightbarbutton is set as:
UIButton *doneBtn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *doneButtonImage = nil;
doneButtonImage = [UIImage imageNamed:#"done.png"];
[doneBtn setImage:doneButtonImage forState:UIControlStateNormal];
doneBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:doneBtn];
self.navigationItem.rightBarButtonItem = doneBarButtonItem;
the width of back is 23px and done is 72px, so they're not equal. Now the issue is I have a label that I want to be in the center all the time.. and I want to clip the text if the text in the label is too long that it interferes with the right bar button. here's how I set it up:
titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
titleLabel.text = self.pageTitle; // Can't change
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:#"PTSans-Narrow" size:30.0];
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
self.navigationItem.titleView = titleLabel;
However this doesn't center the title.. How can I do so?
I suggest first count the width of self.pageTitle
then accordingly set the position of titleLabel.I prefer this:-
titleLabel = [[UILabel alloc] init];
titleLabel.text = self.pageTitle; // Can't change
**CGSize maximumTitleLabelSize = CGSizeMake(200,40);//Set maximum width that an self.pageTitle can have.
CGSize expectedLabelSize = [titleLabel.text titleLabel.font
constrainedToSize:maximumTitleLabelSize
lineBreakMode:UILineBreakModeWordWrap];
//adjust the label the the new width.
CGRect newFrame = titleLabel.frame;
newFrame.size.width = expectedLabelSize.width;
titleLabel.size.width = newFrame.size.width;**
if(titleLabel.size.width==100)
{
titleLabel.frame = cgRectMake(85,5,titleLabel.size.width,30);
}
else if(titleLabel.size.width==200)
{
titleLabel.frame = cgRectMake(36,5,titleLabel.size.width,30);
}
titleLabel.adjustsFontSizeToFitWidth = YES;
titleLabel.clipsToBounds = YES;
titleLabel.numberOfLines = 1;
titleLabel.font = [UIFont fontWithName:#"PTSans-Narrow" size:30.0];
titleLabel.textColor = [UIColor colorWithWhite:0.6 alpha:1.0];
titleLabel.backgroundColor = [UIColor yellowColor];
titleLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight;
titleLabel.textAlignment = UITextAlignmentCenter;
[titleLabel sizeToFit];
titleLabel.frameHeight = self.navigationController.navigationBar.frameHeight;
self.navigationItem.titleView = titleLabel;
Try this.It will help you.Thanks :)
This is the simplest solution, use the sizeToFit method before adding it to the titleView:
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"My Title";
titleLabel.font = [UIFont boldSystemFontOfSize:17.0f];
titleLabel.textColor = [UIColor whiteColor];
[titleLabel sizeToFit];
self.navigationItem.titleView = titleLabel;
Don't use NavigationItem to add left and right bar button, instead use addsubview method to add custom buttons for navigation bar.
Use a tool bar to add multiple buttons to ur navigation bar.
// 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]`enter code here`
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];
you just use this code its work fine try it
UILabel *titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 5, 190, 30)];
titleLabel.textColor = [UIColor whiteColor];
titleLabel.textAlignment = UITextAlignmentCenter;
titleLabel.text =#"My Wish List";
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.font = [UIFont fontWithName:#"Copperplate" size:16.0];
titleLabel.minimumFontSize = 10;
titleLabel.autoresizingMask=UIViewAutoresizingFlexibleWidth;
self.navigationItem.titleView = titleLabel;
[titleLabel release];
i use this code in my project also.......
:-)
I have an UISegmentedControl as the navigation bar's right bar button item. This is achieved by the following code...
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Segment1",#"Segment2",nil]];
[segmentedControl addTarget:self action:#selector(segmentClicked) forControlEvents:UIControlEventValueChanged]; //Where segmentClicked is the method for segment click action
segmentedControl.frame = CGRectMake(0, 0, 90, 35);
UIBarButtonItem *rightBaritem = [ [UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = rightBaritem;
[rightBaritem release];
The above code fine and it will show a segmented Control with two segments "Segment1" & "Segment2".
But I want to show an image instead of Segment1 & a system bar button (Say UIBarButtonSystemItemAdd) instead of Segment2.
Image can be inserted in Segmented control by the code,
UISegmentedControl *segmentedControl = [ [UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:[UIImage imageNamed:#"<image_name.image_type>"],???????,nil]];
But i dont know how to include UIBarButtonSystemItemAdd in the place of ???????.
Thanks in Advance..
This code may solve your problem.This solved my problem that I was facing at the same time.But don't think this is the accurate one.I got the desired which I need to display.This works fine with two buttons.
UISegmentedControl *doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Done"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(240, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Done:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
doneButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObject:#"Close"]];
doneButton.momentary = YES;
doneButton.frame = CGRectMake(10, 7.0f, 70.0f, 30.0f);
doneButton.tintColor = [UIColor blackColor];
[doneButton addTarget:self action:#selector(Krishna:) forControlEvents:UIControlEventValueChanged];
[menustyle addSubview:doneButton];
[doneButton release];
read the following code:
// Creiamo la toolbar sotto
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 372, 320, 44)];
toolbar.tintColor = [UIColor blackColor];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"imgImpostazioniToolbar.png"]];
UIBarButtonItem *pulsanteImpostazioni = [[UIBarButtonItem alloc] initWithCustomView:imageView];
[pulsanteImpostazioni setTarget:self];
[pulsanteImpostazioni setAction:#selector(prova)];
[imageView release];
UIBarButtonItem *spaziatore = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
NSArray *buttons = [[NSArray alloc] initWithObjects:spaziatore, pulsanteImpostazioni, spaziatore, nil];
[toolbar setItems:buttons animated:NO];
[self.view addSubview:toolbar];
[buttons release];
I can see correctly my image but when I try to touch it nothing happen.
I didn't set correctly the selector of "pulsanteImpostazioni"?
Thanks
P.s. "prova" contain only a NSLog.
Try
[pulsanteImpostazioni setAction:#selector(prova:)];
The colon at the end of the selector name matters.
i find the solution in this mode:
UIButton *pulsanteImpostazioni = [UIButton buttonWithType:UIButtonTypeCustom];
[pulsanteImpostazioni setFrame:CGRectMake(0, 0, 200, 50)];
[pulsanteImpostazioni setImage:[UIImage imageNamed:#"imgImpostazioniToolbar.png"] forState:UIControlStateNormal];
[pulsanteImpostazioni addTarget:self action:#selector(prova) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *pulsanteImpostazioni = [[UIBarButtonItem alloc] initWithCustomView:pulsanteImpostazioni];
Thanks anyway for your answers =)
When I first bring up an MKMapView my app seems to pause while it loads. I am assuming this is because it doesn't load asynchronously.
Is there any way I can get around this. While it loads you can't switch tabs or do anything.
Thanks
EDIT: here is my viewDidLoad method:
CGRect bounds = self.view.bounds;
mapView = [[MKMapView alloc] initWithFrame:bounds];
mapView.mapType=MKMapTypeStandard;
mapView.showsUserLocation = YES;
mapView.delegate = self;
[self.view insertSubview:mapView atIndex:0];
NSArray *mapTypes = [NSArray arrayWithObjects:#"Standard",#"Satellite",#"Hybrid",nil];
mapType = [[UISegmentedControl alloc] initWithItems:mapTypes];
[mapType setSegmentedControlStyle:UISegmentedControlStyleBar];
[mapType setCenter:CGPointMake(210, 345)];
[mapType setSelectedSegmentIndex:0];
[mapType setTintColor:[UIColor darkGrayColor]];
[mapType addTarget:self action:#selector(changeType:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:mapType];
// refresh button...
UISegmentedControl *refreshButton = [[[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Refresh",nil]] autorelease];
[refreshButton setSegmentedControlStyle:UISegmentedControlStyleBar];
[refreshButton setCenter:CGPointMake(self.view.frame.size.width-((refreshButton.frame.size.width/2)+5),(refreshButton.frame.size.height/2)+5)];
[refreshButton setMomentary:YES];
[refreshButton setTintColor:[UIColor darkGrayColor]];
[refreshButton addTarget:self action:#selector(updateMarkers) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:refreshButton];
UIBarButtonItem *leftBarButton = [[[UIBarButtonItem alloc] initWithTitle:#"List" style:UIBarButtonItemStylePlain target:self action:#selector(goToList)] autorelease];
[self.navigationItem setRightBarButtonItem:leftBarButton animated:YES];
// refresh label
refreshView = [[UIView alloc] initWithFrame:CGRectMake(5, 5, self.view.frame.size.width-74, 40)];
[refreshView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.8]];
[refreshView.layer setCornerRadius:5];
[self.view addSubview:refreshView];
UILabel *refreshLabel = [[[UILabel alloc] initWithFrame:CGRectMake(5, 5, refreshView.frame.size.width-10, refreshView.frame.size.height-10)] autorelease];
[refreshLabel setTextColor:[UIColor whiteColor]];
[refreshLabel setBackgroundColor:[UIColor clearColor]];
[refreshLabel setFont:[UIFont fontWithName:#"Helvetica" size:12]];
[refreshLabel setNumberOfLines:2];
[refreshLabel setLineBreakMode:UILineBreakModeWordWrap];
[refreshLabel setText:#"Move the map and press 'refresh' to load new results."];
[refreshView addSubview:refreshLabel];
[NSTimer scheduledTimerWithTimeInterval:10 target:refreshView selector:#selector(removeFromSuperview) userInfo:nil repeats:NO];