I know 开发者_如何学Gothat on stackoverflow there are many similar questions but mine is a little different. I'll show this with images, because it's easier to understand. Using this code I took from here, I had this result:
It's easy to see that the second button isn't all on the screen.
I tried to but negative numbers in the UIToolbar
frame but they don't work. I don't need any title there, only a way to move these buttons to the left.
Here is a different approach. I created a custom segmented control (github link) as a UIView
containing several buttons. You could do the same with a space between buttons.
Then I added that as a single button item with a custom view.
CGRect frame = CGRectMake(320-width, 0, width, height);
[segControl.view setFrame:frame];
segControl.view.backgroundColor = [UIColor clearColor];
UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithCustomView:segControl.view];
self.navigationItem.rightBarButtonItem = rightBtn;
The result is able to expand closer to the middle:
Answering the comment below: yes, it's independent.
What's your deadline? This functionality is added in iOS5, so if you're going to release your app after (correct me if I'm wrong) september you can switch your app to the new UIKit. You will lose backwards-compability to <= iOS4 though.
UIToolbar *tool = [UIToolbar new];
tool.frame = CGRectMake(0, 0, 320, 42);
//tool.frame = self.navigationController.navigationBar.frame;
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:7];
//space
UIBarButtonItem *btn3;
btn3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn3];
[btn3 release];
//create setting button
UIButton *bttn=[[UIButton alloc]initWithFrame:CGRectMake(45, 0, 20, 30)];
[bttn setImage:[UIImage imageNamed:@"settings.png"] forState:UIControlStateNormal];
[bttn addTarget:self action:@selector(settings:) forControlEvents:UIControlEventTouchDown];
UIBarButtonItem *btn1=[[UIBarButtonItem alloc]initWithCustomView:bttn];
[items addObject:btn1];
[btn1 release];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] init];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//pause Button
UIButton *bttn1=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 20, 30)];
[bttn1 setImage:[UIImage imageNamed:@"pause.png"] forState:UIControlStateNormal];
[bttn1 addTarget:self action:@selector(pause:) forControlEvents:UIControlEventTouchDown];
//UIBarButtonItem *btn2=[[UIBarButtonItem alloc]initWithCustomView:bttn1];
btnPause = [[UIBarButtonItem alloc]initWithCustomView:bttn1];
[items addObject:btnPause];
//[btn2 release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Space
btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:btn];
[btn release];
//Next Button
btnNext = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:
UIBarButtonSystemItemPlay target:self action:@selector(next:)];
[items addObject:btnNext];
[tool setItems:items];
tool.barStyle =UIBarStyleDefault;
tool.backgroundColor = [UIColor clearColor];
//self.navigationItem.leftBarButtonItem.customView = tool;
//self.navigationItem.titleView = tool;
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:tool];
use this method to display more buttons in toolbar with navigation.
精彩评论