【问题标题】:How do I add a button on right side of the toolbar in a UISplitViewController?如何在 UISplitViewController 的工具栏右侧添加一个按钮?
【发布时间】:2011-06-14 11:06:44
【问题描述】:

尝试在基于拆分视图的应用程序中向 detailviewcontroller 工具栏的右侧添加一个按钮。我使用灵活的空间将其移到右侧。在纵向它工作正常,但在横向(当菜单按钮消失时),按钮被移动,使其一半离开屏幕。

这是相关代码(DetailViewController.m):

- (void) viewDidLoad 
{
    // initialize toolbar
    toolbar = [[UIToolbar alloc] initWithFrame: CGRectMake( 0, 0, 768, 44 )];
    titleLabel = [[UILabel alloc] initWithFrame: CGRectMake( 284, 3, 200, 35 )];
    titleLabel.text = @"Title & Location";
    titleLabel.textAlignment = UITextAlignmentCenter;
    [toolbar addSubview: titleLabel];
    UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem: UIBarButtonSystemItemFlexibleSpace target: nil action: nil];
    UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle: @"Add Event" style: UIBarButtonItemStyleDone target: rootController action: @selector(parseDone)];
    NSArray *buttonArray = [NSArray arrayWithObjects: flexibleSpace, doneButton, nil];
    [toolbar setItems: buttonArray];
    [doneButton release];
    [flexibleSpace release];
    [self.view addSubview: toolbar];
}
- (void)showRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem 
{
    NSMutableArray *itemsArray = [toolbar.items mutableCopy];
    [itemsArray insertObject: barButtonItem atIndex: 0];
    [toolbar setItems:itemsArray animated:NO];
}
- (void)invalidateRootPopoverButtonItem:(UIBarButtonItem *)barButtonItem 
{
    NSMutableArray *itemsArray = [toolbar.items mutableCopy];
    [itemsArray removeObject:barButtonItem];
    [toolbar setItems:itemsArray animated:NO];
    [itemsArray release];
}

【问题讨论】:

  • 这个问题很可能是由于自动调整掩码。您可能需要检查设置并确保它们是您想要的。
  • 我过去也遇到过这个问题,我已经以某种方式解决了它,但我的记忆就像一个漏勺。
  • 另外,您已经提出了 14 个问题并接受了 0 个答案。您可能想回顾旧问题并接受(单击旁边的复选标记)解决您的问题的答案。通过这样做,您将成为 SO 社区的活跃成员,并且更有可能鼓励人们在未来花时间尝试帮助您。

标签: iphone uisplitviewcontroller uibarbuttonitem uitoolbar


【解决方案1】:

我从不久前从事的一个项目中认识到这个问题。我认为它只是偶尔发生,我不记得我们是否真的修复了它。

在您的情况下,似乎只使用导航栏并设置rightBarButtonItemleftBarButtonItem 会更容易。这应该可以解决您的问题。

【讨论】:

    【解决方案2】:

    事实上,如果您将视图控制器放在 UINavigationController 中,您将获得 UINavigationBar 以及导航控制器的所有功能(如果您选择使用它)。

    【讨论】:

      【解决方案3】:

      问题是当 iPad 旋转到横向模式时我没有正确调整工具栏的大小。通过添加此代码修复了问题:

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
      if( interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight ) {
          [toolbar setFrame: CGRectMake( 0, 0, 700, 44 )];
      }
      else {
          [toolbar setFrame: CGRectMake( 0, 0, 768, 44 )];
      }
      
      return YES;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2022-01-05
        • 1970-01-01
        • 1970-01-01
        • 2015-10-04
        • 1970-01-01
        • 1970-01-01
        • 2018-04-22
        • 1970-01-01
        相关资源
        最近更新 更多