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應用程序,它有一個固定的橫向

,我發現我有硬編碼的大小的寬度