5

典型的NavController行爲是在端口/橫向時調整大小。從44px高度到32px我相信(從我的頭頂上)。將UINavigationB自動調整爲橫向旋轉

我正在使用故事板並將導航欄包含在VC中,而不是控制器中。在縱向和橫向之間旋轉時,導航欄不會自動調整大小。

我看到這個答案,但是這並不能幫助:Resizing UINavigationBar on rotation

導航欄是通過外觀控制器,它是在App代表加載:

UIImage *portraitImage = [UIImage imageNamed:@"mainNavBar"]; 
UIImage *landscapeImage = [UIImage imageNamed:@"mainNavBarLandscape"]; 
[[UINavigationBar appearance] setBackgroundImage:portraitImage forBarMetrics:UIBarMetricsDefault]; 
[[UINavigationBar appearance] setBackgroundImage:landscapeImage forBarMetrics:UIBarMetricsLandscapePhone]; 

[[UINavigationBar appearance] setBackgroundColor:[UIColor blackColor]]; 
[[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]]; 

[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin]; 

不幸的是,最後沒有什麼區別和因爲導航欄將包含在多個控制器中,所以我不想在上面的問題答案中重複相同的粘合代碼,以便我的所有VC在旋轉時調整大小。

回答

3
[[UINavigationBar appearance] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin]; 

但是你沒有說靈活的高度。據我所知,高度是你希望變得靈活的特徵。

+0

正確,我也注意到,發佈後不久。謝謝 – StuartM 2013-04-30 17:31:55

0

接受的答案適用於我,但我想移動到完全基於約束的佈局。

設置UIBarPosition上的UINavBar委託人根據需要定位事物。記得在IB中附上代表(當然)。

// In my view controller 

#pragma mark - UIBarPositioningDelegate 
- (UIBarPosition)positionForBar:(id<UIBarPositioning>)bar { 
    return UIBarPositionTopAttached; 
} 

有關在IB的UINavBar約束:

  • 頂層空間到:頂面佈置指南(0)
  • 前導空格到:上海華(0)
  • 訓練間隙到:上海華(0)
  • 最重要的是....沒有任何高度限制! -

高度將由運行時的內容大小決定。原本我無法正確地完成這項工作,因爲我的縱向內容擁抱優先級爲750,但這是錯誤的。當我將其更改回默認(250)時,它工作正常。

+0

以上似乎不會調整工具欄的大小。我相信它應該在≤4英寸的設備上高32英寸。這種技術似乎只關心調整工具欄的位置以查看是否存在狀態欄。 – 2016-03-22 14:25:44

5

如果您正在使用自動佈局,UINavigationBarintrinsicContentSize在旋轉時會正確更新,但其高度NSContentSizeLayoutConstraint不會。調用invalidateIntrinsicContentSize手動指示自動佈局更新導航欄的高度約束。

// View controller 
public override func viewWillTransition(to size: CGSize, with coordinator: UIViewControllerTransitionCoordinator) 
{ 
    super.viewWillTransition(to: size, with: coordinator); 

    self.navigationBar?.invalidateIntrinsicContentSize(); 
}