Iphone UIToolbar作为UINavigationbar中的右按钮,包含两个按钮,但它们之间没有间隔

Iphone UIToolbar作为UINavigationbar中的右按钮,包含两个按钮,但它们之间没有间隔,iphone,objective-c,ios,ipad,Iphone,Objective C,Ios,Ipad,我想让我的右栏按钮包含2个按钮。因此,我使用UIToolbar实现了这一点。但问题是这两个按钮彼此分开,而我想要达到的效果是它们彼此齐平 这是他们现在的样子 下面是我目前用来实现这些按钮的代码 UIToolbar *tools = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 2.0f, 120.0f, 40.01f)]; // 44.01 shifts it up 1px for some reason tools.clearsC

我想让我的右栏按钮包含2个按钮。因此,我使用UIToolbar实现了这一点。但问题是这两个按钮彼此分开,而我想要达到的效果是它们彼此齐平

这是他们现在的样子

下面是我目前用来实现这些按钮的代码

    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];
有谁能告诉我如何让这两个按钮并排而不留空隙

非常感谢,,
-代码

您可以将UISegmentedControl作为customView添加到navigationItem中,并将其瞬时属性设置为true,这样您就可以得到您想要的

下面是示例代码

- (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;
    }

}
这是它的样子

在这里,您可以在控制台中看到事件

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

请看一下本教程,也许它有助于:


在两个按钮之间添加一个空格。 两个这样做

UIBarButtonItem*noSpace=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:uiBarbuttonSystemFixedSpace目标:nil操作:nil]自动释放]; noSpace.width=-10.0

将此noSpace对象插入到您的阵列(案例中的按钮)。在两个uibarbuttonite对象之间


这样就可以了。

不用
UIToolBar
您可以使用
UIView
将其添加为自定义视图添加到barbutton。