【问题标题】:Change the height of NavigationBar and UIBarButtonItem elements inside it in Cocoa Touch在 Cocoa Touch 中更改 NavigationBar 和 UIBarButtonItem 元素的高度
【发布时间】:2011-11-27 13:01:00
【问题描述】:

我想这并不完全符合 Apple 的指导方针,但我想它一定是有可能的。我想更改 UINavigationController 内导航栏的高度和该栏内 UIBarButtonItem 元素的高度。

使用this question 的技巧,我设法改变了导航栏的高度,但我看不到调整栏按钮项高度的方法。

如果有人知道如何更改条形按钮项目的大小,请帮助我。

【问题讨论】:

标签: cocoa-touch uinavigationcontroller uinavigationbar uibarbuttonitem


【解决方案1】:

这是我的解决方案。效果很好。

@interface UINavigationBar (CustomHeight)

@end

@implementation UINavigationBar (CustomHeight)

- (CGSize)sizeThatFits:(CGSize)size {
    // Change navigation bar height. The height must be even, otherwise there will be a white line above the navigation bar.
    CGSize newSize = CGSizeMake(self.frame.size.width, 40);
    return newSize;
}

-(void)layoutSubviews {
    [super layoutSubviews];

    // Make items on navigation bar vertically centered.
    int i = 0;
    for (UIView *view in self.subviews) {
        NSLog(@"%i. %@", i, [view description]);
        i++;
        if (i == 0)
            continue;
        float centerY = self.bounds.size.height / 2.0f;
        CGPoint center = view.center;
        center.y = centerY;
        view.center = center;
    }
}

【讨论】:

  • i 永远不是 0,为什么要检查呢?
  • 这会改变所有导航栏的高度。如果我想控制是否改变高度怎么办?
  • @Gon 你需要继承 UINavigationBar 并重载 sizeThatFits: 和 layoutSubviews 方法
  • 然后你可以选择子类改变高度或超类不改变。知道了。谢谢@VinceYuan
【解决方案2】:

也许这个关于自定义导航栏的教程会对你有所帮助:Recreating the iBooks wood themed navigation bar

如果您使用UIImageView 创建BarButtonItem,则可以更改自定义 UIImageView 的帧大小/边界大小

UIImageView* imageView = [[[UIImageView alloc] initWithFrame:navigationController.navigationBar.frame] autorelease];
imageView.contentMode = UIViewContentModeLeft;
imageView.image = [UIImage imageNamed:@"NavBar-iPhone.png"];
[navigationController.navigationBar insertSubview:imageView atIndex:0];

因此,根据您的需要,您可以为 -initWithFrame 方法提供适当的值。

【讨论】:

  • +1; mgamer 肯定需要在此处使用自定义图像或视图。标准的按钮项只是没有可变的高度。
【解决方案3】:
static CGFloat const CustomNavigationBarHeight = 74;


@implementation WTNavigationBar



- (CGSize)sizeThatFits:(CGSize)size{
    size.width = 1024;
    size.height = CustomNavigationBarHeight;
    return size;
}

-(void)layoutSubviews {
    [super layoutSubviews];
    for (UIView *view in self.subviews) {
        SFLog(@"view.class=%@",[view class]);
        if ([view isKindOfClass:NSClassFromString(@"UINavigationItemButtonView")]) {
            float centerY = self.bounds.size.height / 2.0f;
            CGPoint center = view.center;
            center.y = centerY;
            view.center = center;
        }
    }
}


@end

在我的 iPad 应用程序中,它有一个固定的横向方向,我发现我必须硬编码尺寸的宽度

【讨论】:

    【解决方案4】:

    我设法通过继承UINavigationBar 并覆盖-layoutSubviews 来做类似的事情。代码如下:

    -(void)layoutSubviews {
        [super layoutSubviews];
        int i = 0;
        for (UIView *view in self.subviews) {
            NSLog(@"%i. %@", i++, [view description]);
            if ([view isKindOfClass:NSClassFromString(@"UINavigationButton")]) {
                view.frame = CGRectMake(0, 0, 100, 50);
            }
        }
    }
    

    如果您需要知道如何继承UINavigationBar,请查看this very good answer

    我不太确定 NSClassFromString(@"UINavigationButton")] 部分。它有效,但我这样做是作为一个实验,我不确定这是否会得到 Apple 的批准。我希望有更好的知识的人可以提供一些启示。

    【讨论】:

      【解决方案5】:

      对于 UINavigationbar

      在 iOS SDK 4.3 及更高版本中,有一种方法(hack)可以更改 UINavigationBar 的高度。

      要更改 UINavigationController 的高度,请在 viewWillAppear:animated: 函数中更改其框架大小。然后,高度将在整个应用程序中保持自定义。

      对于 UIBarButtonItems
      实际上我自己也遇到过这个问题,我唯一能想到的就是利用 initWithCustomView 并传入一个带有定义框架的 UIButton。

      UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
      
      /*
      * Insert button styling
      */
      
      button.frame = CGRectMake(0, 0, width, height);
      
      UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
      

      否则 UIBarButtonItem 只有一个可以设置的宽度属性,但不幸的是没有高度属性。我用 initWithCustomView 完成的另一件漂亮的事情是传递一个带有按钮和其他东西(如活动指示器)的工具栏。希望这会有所帮助。

      【讨论】:

        【解决方案6】:

        你有多想要这个?而且,你想让你的导航栏有多薄(或多厚)?

        一种方法是将导航栏的变换设置为缩放并翻译它。如果缩放太多,标题和按钮文本会看起来很奇怪,但如果你只需要刮掉几个像素就可以了。

        这是将其缩放为全高的 75%(33 像素高)的结果:

        以及产生这个的代码:

        - (void)viewDidLoad
        {
            [super viewDidLoad];
        
            self.navigationItem.title = @"Thin Navigation Bar";
        
            self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle: @"Press Me" style:UIBarButtonItemStyleBordered target: nil action: NULL ] autorelease];
        
            CGFloat scale = .75;
            CGFloat cy = 44.0 - ( 44.0 * scale );
        
            self.navigationController.navigationBar.transform = CGAffineTransformScale( CGAffineTransformMakeTranslation( 0, -cy / 2.0 ), 1.0, scale )  ;
        }
        
        - (void)viewDidAppear:(BOOL)animated
        {
            [super viewDidAppear:animated];
        
            CGFloat scale = .75;
            CGFloat cy = 44.0 - ( 44.0 * scale );
        
            CGRect r = self.view.frame;
            r.origin.y -= cy;
            r.size.height += cy;
            self.view.frame = r;
        }
        

        现在,这确实存在许多问题,这些问题可能解决也可能无法解决。 #1 是您正在使用 UINavigationController 来调整导航栏和视图控制器视图的大小和位置。使用这种技术的视图控制器之间的动画可能看起来很奇怪。

        如果您能解决相关问题,我会很好奇...

        最后一个想法:如果你不使用 UINavigationController,那么除了压扁的文本之外,真的没有很多问题。或者,您可以使用导航控制器但隐藏默认导航栏,并将细导航栏添加到您的每个子视图控制器视图。您甚至可以继承 UINavigationBar 并从内部设置转换:

        @interface TSThinNavBar : UINavigationBar
        {
        }
        @end
        
        @implementation TSThinNavBar
        
        // assuming we'll always be constructed from a nib
        - (id) initWithCoder:(NSCoder *)aDecoder
        {
            self = [super initWithCoder: aDecoder];
            if ( self != nil ) 
            {
                CGFloat scale = .75;
                CGFloat cy = 44.0 - ( 44.0 * scale );
        
                self.transform = CGAffineTransformScale( CGAffineTransformMakeTranslation( 0, -cy / 2.0 ), 1.0, scale )  ;
            }
        
            return self;
        }
        
        @end
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-05-19
          • 1970-01-01
          • 2015-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-12-24
          相关资源
          最近更新 更多