1

我已經子類UINavigationBar在我的應用程序中有一個自定義導航欄。該條在多個視圖控制器上重複使用,具有與左側按鈕項相同的樣式菜單按鈕,這就是爲什麼它被分類的原因。使用按鈕處理子類UINavigationBar的旋轉

然後將子類添加到故事板中的視圖控制器導航欄中。

這是UINavigationBar子類中的代碼:

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    UIImage *image = [[UIImage imageNamed: @"mainNavBar"] resizableImageWithCapInsets:UIEdgeInsetsMake(0,5,0,5)]; 
    [image drawInRect:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)]; 
} 

-(void)awakeFromNib { 
    UIButton *leftButton = [[UIButton alloc]initWithFrame:CGRectMake(7.0f, 7.0f, 38.0f, 29.0f)]; 
    [leftButton setImage:[UIImage imageNamed:@"menuBarItem"] forState:UIControlStateNormal]; 
    [leftButton addTarget:nil action:@selector(menuItemPressed:) forControlEvents:UIControlEventTouchUpInside]; 
    [self addSubview:leftButton]; 
} 

的問題是設備的旋轉。我也有一些代碼在使用appDidFinishLaunching外觀API,用於額外的設置:

// Custom Navigation Bar appearance setup 
[[UINavigationBar appearance] setTitleTextAttributes: [NSDictionary dictionaryWithObjectsAndKeys: 
                 [UIColor colorWithRed:245.0/255.0 green:245.0/255.0 blue:245.0/255.0 alpha:1.0], UITextAttributeTextColor, 
                 [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5],UITextAttributeTextShadowColor, 
                 [NSValue valueWithUIOffset:UIOffsetMake(0, 1)], 
                 UITextAttributeTextShadowOffset,nil]]; 
// Used to deal with the rotation of the nav bar when implemented outside of Navigation Controller 
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight]; 
[[UINavigationBar appearance] setContentMode:UIViewContentModeScaleAspectFit]; 
// Used to push the title down slightly when in Landscape and NavBar outside of Navigation Controller 
[[UINavigationBar appearance] setTitleVerticalPositionAdjustment:2 forBarMetrics:UIBarMetricsLandscapePhone]; 

當裝置轉動時,它的所有工作正常,在高度欄縮放方面如預期從44至32PX。但是,按鈕仍然顯示在欄的旁邊。 enter image description here

我已經經歷了這方面的一些其他SO帖子看了,但找不出如何正確地完成此:iPhone: UINavigationBar with buttons - adjust the height

UINavigationBar autoresizing

理想的情況下,我不希望有應對的自動旋轉一個視圖控制器,因爲這個UINavigationBar通過大量的VC重新使用。添加自動輪轉代碼意味着每個VC都可能也需要這樣的代碼?

編輯 - 以下的答案 如果我改變awakeFromNib包括以下內容:

UINavigationItem* ni = [[UINavigationItem alloc] initWithTitle:@"Test"]; 
    UIImage *menuBgImage = [UIImage imageNamed:@"menuBarItem"]; 
    UIBarButtonItem *b =[[UIBarButtonItem alloc] initWithImage:menuBgImage landscapeImagePhone:menuBgImage style:UIBarButtonItemStylePlain target:nil action:@selector(menuItemPressed:)]; 
    ni.leftBarButtonItem = b; 
    self.items = @[ni]; 

我有UIBarButtonItemStylePlain的破壞形象的問題: enter image description here

如果我完成之前設置了initWithCustomView並使用原始代碼中的leftButton。隨着leftButton使用高度自動調整大小 - 然後在橫向上獲得嚴重拉伸的圖像?

enter image description here

EDIT 2 - 每答案 enter image description here

+0

您必須設置在awakeFromNib方法中創建的UIbButton的自動調整大小,並啓用navigatonBar的autoresizesubviews屬性。 – fibnochi 2013-05-03 16:19:39

+0

您能否確認如何在答案中完成此操作? – StuartM 2013-05-03 16:37:58

+0

***非法屬性類型,c在調用選擇器中,_UIAppearance_setAutoresizesSubviews:當試圖設置'[[UINavigationBar appearance] setAutoresizesSubviews:YES];' – StuartM 2013-05-03 16:42:00

回答

0
[self addSubview:leftButton]; 

這就是問題的其他圖像。這不是你如何將東西添加到UINavigationBar!你必須從一個UINavigationItem開始,並將其按鈕設置爲一個UIBarButtonItem。導航欄大小調整後,酒吧按鈕項目將被調整。

這裏是讓你開始一個簡單的例子(可以在你的awakeFromNib去):

UINavigationItem* ni = [[UINavigationItem alloc] initWithTitle:@"Test"]; 
UIBarButtonItem* b = [[UIBarButtonItem alloc] initWithTitle:@"Howdy" 
    style:UIBarButtonItemStyleBordered 
    target:self action:@selector(pushNext:)]; 
ni.leftBarButtonItem = b; 
self.items = @[ni]; 

但是,請注意,這是去工作,如果你再使用這個UINavigationBar的作爲的一部分UINavigationController的!視圖控制器的navigationItem將推動左側的酒吧按鈕。如果這是標準導航界面的一部分,只需讓每個導航控制器添加左欄按鈕項。

此外,你不應該實施drawRect。改爲設置導航欄的背景圖片。

+0

你知道更多從UINavigationItem開始,在外觀上不可用API嗎? – StuartM 2013-05-03 16:34:36

+0

我不明白如何訪問由UINavigationBar的子類內的子類創建的UINavigationItem。請讓我知道任何其他信息 – StuartM 2013-05-03 16:38:42

+0

你也沒有使用外觀API來做'addSubview'。你只是在子類中做。所以我說的是,而不是那樣,做正確的方法:做一個UINavigationItem,設置它的'leftBarButtonItem',並把它推到導航欄上。 – matt 2013-05-03 16:40:25