2

我想让我的右栏按钮包含 2 个按钮。所以我通过使用 UIToolbar 来做到这一点。但问题是 2 个按钮彼此分开,而我想要达到的效果是让它们彼此齐平。

这是他们现在的样子

在此处输入图像描述

这是到目前为止我用来实现按钮的代码

    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];

任何人都可以告诉我如何让这两个按钮没有任何间隙地坐在彼此旁边吗?

非常感谢,-代码

4

3 回答 3

2

您可以将 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
于 2012-11-25T14:46:02.507 回答
0

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

UIBarButtonItem *noSpace = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease]; noSpace.width = -10.0;

将此 noSpace 对象插入到您的数组中(在您的情况下为按钮)。UIBarButtonItem 的两个对象之间。

会好的。。

于 2013-02-15T05:25:34.547 回答
0

看看这个教程,也许它会有所帮助:

Xcode:导航栏中的多个按钮

于 2012-11-25T15:16:30.660 回答