【问题标题】:UINavigationBar with two buttons on the leftUINavigationBar 左侧有两个按钮
【发布时间】:2011-07-26 09:54:25
【问题描述】:

我知道在 stackoverflow 上有很多类似的问题,但我的问题有点不同。 我将用图像来展示这一点,因为它更容易理解。 使用我从中获取的这段代码 here,我得到了这个结果:

很容易看出,第二个按钮并未全部显示在屏幕上。 我试图在UIToolbar 框架中添加负数,但它们不起作用。我不需要任何标题,只需要一种将这些按钮向左移动的方法。

【问题讨论】:

  • 检查这似乎是同样的问题stackoverflow.com/questions/3789283/…
  • 如果您将之前的控制器标题更改为仅显示 Exams 而不是 Blood Exams,是否会发生同样的情况?我很好奇,因为我认为问题在于左侧按钮的宽度,以及您为容纳额外的两个按钮而创建的 UIToolBar 的宽度。
  • '有很多问题,但这是我的问题'。抱歉,无法抗拒;)
  • @edo42,我喜欢你提供的代码。
  • @Toro 但它们不能按我的意愿工作

标签: iphone objective-c cocoa-touch uinavigationbar uibarbuttonitem


【解决方案1】:

这是一种不同的方法。我创建了一个custom segmented control(github 链接)作为包含几个按钮的UIView。你也可以在按钮之间留一个空格。

然后我将其添加为带有自定义视图的单个按钮项。

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;

结果可以向中间扩展:

回答下面的评论:是的,它是独立的。

【讨论】:

【解决方案2】:

你的截止日期是什么时候?这个功能是在 iOS5 中添加的,所以如果你打算在 9 月之后发布你的应用程序(如果我错了,请纠正我)你可以将你的应用程序切换到新的 UIKit。但是,您将失去对

【讨论】:

  • 你说得对,我没注意到。但我可能需要在 iOS 5 之前这样做
  • 我能问个问题吗?为这个案例添加的新功能在 UIKit 中的哪个类?
  • 除了UINavigationBar.leftBarButtonItem,iOS5 中还有UINavigationItem.leftBarButtonItems(注意s)。
【解决方案3】:
 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];

使用此方法在带有导航的工具栏中显示更多按钮。

【讨论】:

  • 十一个弹性空间按钮有什么需要?将其称为弹性空间是有充分理由的。
  • 但我也需要左键:-(
  • 我现在展示了你可以从这段代码中获得帮助的方法。我不知道你会做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-03
  • 1970-01-01
  • 2015-09-21
  • 2014-12-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多