【问题标题】:iPhone: UINavigationBar with buttons - adjust the heightiPhone:带按钮的 UINavigationBar - 调整高度
【发布时间】:2010-12-09 20:30:37
【问题描述】:

我正在开发一个可以在两个方向上工作的 iPhone 应用程序:纵向和横向。

我正在使用嵌入在 UINavigationController 中的一个视图和表格视图。此导航栏及其按钮的高度是:44px 纵向或 34px 横向。

在不同的视图中,我自己创建了 UINavigationBar,并且能够将框架设置为正确的大小,但带有 UIBarButtonItem 的嵌入式 UINavigationItem 不会缩小。所以对于横向模式下的 34 像素,这个按钮太大并且在高度上与导航栏重叠。

有趣的是,它在其他应用程序中使用相同的代码......不知道它不在这里。

是否可以调整 UIBarButtonItem 的高度/位置?

这里是sn-p的代码:

    navBar = [[UINavigationBar alloc] initWithFrame: CGRectMake(0.0f, 0.0f, 320.0f, 34.0f)];
[navBar setBarStyle: UIBarStyleBlackOpaque];

[self addSubview: navBar];

barButton = [[UIBarButtonItem alloc] initWithTitle: NSLocalizedString(@"flip", @"flip") style:UIBarButtonItemStylePlain target:self action:@selector(flip)];

item = [[UINavigationItem alloc] initWithTitle: NSLocalizedString(@"Translation", @"Translation Tab Bar Title")];
[item setRightBarButtonItem: barButton];
[navBar pushNavigationItem:item animated:NO];   

alt text http://labs.kiesl.eu/images/navbar.png

谢谢

汤姆

【问题讨论】:

    标签: iphone height uinavigationbar uibarbuttonitem uinavigationitem


    【解决方案1】:

    创建备用图像,并在处理旋转动画的视图控制器方法中,更改条形按钮项中的图像。或者您可以使用 UIImageView 作为项目中的自定义视图,并将其内容模式设置为缩小图像。

    【讨论】:

      【解决方案2】:

      我想通了:导航栏的高度一定是32px!使用 33 或 34 像素时,对齐会出错。

      【讨论】:

      • 非常有帮助,谢谢。 32px 是关键。我使用的是 34px,但它不起作用。
      【解决方案3】:

      这是我根据 crashtesttommy 的回答编写的代码:

      - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
          return YES;
      }
      
      - (void) correctNavBarHeightForOrientation:(UIInterfaceOrientation)orientation {
          // This is only needed in on the iPhone, since this is a universal app, check that first.
          if ([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPhone){
              if (UIInterfaceOrientationIsLandscape(orientation)) {
                  self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 32.0f);
              } else {
                  self.navBar.frame = CGRectMake(self.navBar.frame.origin.x, self.navBar.frame.origin.y, self.navBar.frame.size.width, 44.0f);
              }
          }  
      }
      
      - (void) viewWillAppear:(BOOL)animated {
          [self correctNavBarHeightForOrientation:[UIApplication sharedApplication].statusBarOrientation];
      }
      
      - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
          [self correctNavBarHeightForOrientation:toInterfaceOrientation];
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-03
        • 1970-01-01
        • 1970-01-01
        • 2014-11-15
        • 1970-01-01
        • 2011-01-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多