19

我想这不是严格符合Apple的指导原则,但我想这肯定是可能的。我想改变UINavigationController中导航栏的高度和该栏内UIBarButtonItem元素的高度。在Cocoa Touch中更改里面的NavigationBar和UIBarButtonItem元素的高度

使用从this question的把戏我设法改变导航栏的高度,但我看不到调整栏按钮项目的高度。

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

+0

是它也许并不容易,只需创建自己的自定义导航条和相关条项目?有关详细信息,请参阅此链接上的答案:http://stackoverflow.com/questions/6398869/how-to-assign-my-customnavigationbar-to-a-uinavigationcontroller/6398891#6398891 – Luke

回答

16

也许本教程的自定义导航栏将帮助您:Recreating the iBooks wood themed navigation bar

如果您创建了一个UIImageView一个BarButtonItem你也许可以更改自定义的UIImageView的框架尺寸/ boundsize

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方法适当的值。

+0

+1; mgamer绝对需要在这里使用自定义图像或视图。标准的按钮项目没有可变的高度。 –

3

我设法做类似的事情,通过继承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的批准。我希望有更多知识的人可以发表一些看法。

3

对于UINavigationBar的

在iOS中SDK 4.3及以后,有一种方法(劈)来改变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只可以设定可惜没height属性width属性。我使用initWithCustomView完成的另一件漂亮的事情是在工具栏中传递一个按钮和其他事物,如活动指示器。希望这可以帮助。

2

你想要这个怎么样?而且,你想让你的导航栏变得多薄(或厚)?

一种方法是设置导航栏的变换来缩放和翻译它。如果缩放太多,标题和按钮的文字看起来会不可思议,但如果您只需要刮几个像素,您可能就会好起来。

这里的缩放它的结果是全高度(33个像素高)的75%:

enter image description here

而生成此代码:

- (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 
18

这是我的解决方案。它工作得很好。

@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; 
    } 
} 
+1

伟大的解决方案! – Oleg

+0

非常好,非常感谢你。 – Sabobin

+2

我从来没有0,为什么检查? – pfrank

4
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应用程序,它有一个固定的横向

,我发现我有硬编码的大小的宽度