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.......
:-)
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 need it aligned to left. Or there is no way and I need to add custom label?
Add it to the left button item:
UIView *mCustView = [[UIView alloc] initWithFrame:CGRectMake(0,0,200,20)];
mCustView.backgroundColor = [UIColor redColor];
UILabel *mTextLabel = [[UILabel alloc] initWithFrame:CGRectMake(0,0,200,20)];
mTextLabel.backgroundColor = [UIColor blueColor];
mTextLabel.font = [UIFont systemFontOfSize:20];
mTextLabel.textColor = [UIColor blackColor];
mTextLabel.text = #"Random text tested here, so sit back and enjoy";
[mCustView addSubview:mTextLabel];
[mTextLabel release];
UIBarButtonItem *mCustomBarItem = [[UIBarButtonItem alloc] initWithCustomView:mCustView];
self.navigationItem.leftBarButtonItem = mCustomBarItem;
[mCustView release];
You need to add a custom view to the navigation bar's -titleView
self.navigationItem.titleView = someLabel;
I think you could use UIBarButtonSystemItemFixedSpace which provides a way of adding a fixed padding in a navigation bar:
UIBarButtonItem *fixedSpaceItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace
target:nil
action:NULL]
autorelease];
fixedSpaceItem.width = 50;
UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:nil
action:NULL]
autorelease];
// vc is some view controller
vc.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:
fixedSpaceItem,
cancelItem,
nil];
This will right indent the "Cancel" button 50 points.
I have found the solution by myself:
float offset = 210.0f;
UIToolbar* tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, offset, 44.01)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tools];
[tools release];
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];
I have UISegmentedControl on toolbar, when I add custom background drawing method for toolbar segmented control moved nearly 2px down, so it is not vertically centered. Here is corresponding image link, see toolbar on the bottom of the screenshot and code how I've added segmented control to toolbar:
segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentedItems];
[segmentedControl addTarget:self action:#selector(tabChanged:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *button1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
button1.style = UIBarButtonItemStylePlain;
UIBarButtonItem *button2 = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
button2.width = kSegmentedControlFrame.size.width;
button2.style = UIBarButtonItemStylePlain;
UIBarButtonItem *button3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
button3.style = UIBarButtonItemStylePlain;
CGFloat y = self.contentView != nil ? self.contentView.frame.origin.y + self.contentView.frame.size.height : kToolbarFrame.origin.y;
UIToolbar *t = [[[UIToolbar alloc] initWithFrame:CGRectMake(kToolbarFrame.origin.x, y,
kToolbarFrame.size.width, kToolbarFrame.size.height)] autorelease];
[t setItems:[NSArray arrayWithObjects:button1, button2, button1 , nil]];
t.clipsToBounds = NO;
t.autoresizesSubviews = YES;
t.clearsContextBeforeDrawing = NO;
t.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleWidth
| UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin;
t.barStyle = UIBarStyleDefault;
t.tintColor = [UIColor clearColor];
t.contentMode = UIViewContentModeScaleToFill;
t.backgroundColor = [UIColor clearColor];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [ColorUtil colorWithRed:129 withGreen:167 withBlue:186];
segmentedControl.frame = kSegmentedControlFrame; //CGRectMake(kSegmentedControlFrameX, kSegmentedControlY, kSegmentedControlWidth, kSegmentedControlHeight);
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.contentVerticalAlignment = UIControlContentVerticalAlignmentTop;
segmentedControl.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
segmentedControl.clipsToBounds = NO;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleBottomMargin;
segmentedControl.center = CGPointMake(160, 22);
segmentedControl.momentary= NO;
[button1 release];
[button2 release];
[button3 release];
return t;
Thanks in advance,
Naira
Is it possible that the toolbar is extending off the bottom of the screen, so that the segmented controller only appears closer to the bottom? There's no clear guard here against having a content view and toolbar size that together are taller than the visible screen area. Try positioning it with an origin.y value that is something along the lines of
CGFloat y = self.view.bounds.size.height - kToolbarFrame.size.height;
I created a nav bar and a label programmatically in my view controller. Now I want to add a "done" button but cant seem to find a way without using IB....is there any way to do that?
Here is my view controller's viewDidLoad:
//Adding navBar programmatically
CGFloat width = self.view.frame.size.width;
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0,width,52)];
navBar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
[self.view addSubview:navBar];
//Adding label to navBar programmatically
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(10,2,width-20,14)];
label.autoresizingMask = UIViewAutoresizingFlexibleWidth;
label.text = #"TITLE";
label.backgroundColor = [UIColor clearColor];
label.font = [UIFont systemFontOfSize:22];
label.shadowOffset = CGSizeMake(1,1);
label.textColor = [UIColor whiteColor];
label.textAlignment = UITextAlignmentCenter;
[navBar addSubview:label];
//Adding back button to navBar programmatically
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStylePlain target:self action:#selector(dismissView:)];
self.navigationItem.leftBarButtonItem = rightButton;
Last part obviously doesnt work...
Thanks for any help!
Why are you not using a UIToolBar instead?
To add buttons to a UINavigationBar, you need to create a UINavigationItem. If you're using UINavigationBar as part of UINavigationController (as is the common case), this is done automatically, as a standalone view, you have to do this yourself.
UINavigationItem *item = [[[UINavigationItem alloc] initWithTitle:#""] autorelease];
item.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismissView:)] autorelease];
[navBar setItems:[NSArray arrayWithObject:item] animated:NO];
The navigationItem is only used when you push a viewcontroller in a navigationcontroller
in your case you need to use
-pushNavigationItem:animated:
on the UINavigationBar
Check the docs for more details:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UINavigationBar_Class/Reference/UINavigationBar.html