我已經子類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。但是,按鈕仍然顯示在欄的旁邊。
我已經經歷了這方面的一些其他SO帖子看了,但找不出如何正確地完成此:iPhone: UINavigationBar with buttons - adjust the height
理想的情況下,我不希望有應對的自動旋轉一個視圖控制器,因爲這個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的破壞形象的問題:
如果我完成之前設置了initWithCustomView並使用原始代碼中的leftButton。隨着leftButton使用高度自動調整大小 - 然後在橫向上獲得嚴重拉伸的圖像?
EDIT 2 - 每答案
您必須設置在awakeFromNib方法中創建的UIbButton的自動調整大小,並啓用navigatonBar的autoresizesubviews屬性。 – fibnochi 2013-05-03 16:19:39
您能否確認如何在答案中完成此操作? – StuartM 2013-05-03 16:37:58
***非法屬性類型,c在調用選擇器中,_UIAppearance_setAutoresizesSubviews:當試圖設置'[[UINavigationBar appearance] setAutoresizesSubviews:YES];' – StuartM 2013-05-03 16:42:00