2017-09-13 98 views
10

如何爲iOS 11中的大標題NavigationBar設置自定義背景圖像?我正在使用我已經分配給storyboard中的navigationControllers的自定義子類。大標題的自定義背景圖像iOS 11中的NavigationBar 11

這是我如何創建我的自定義的NavBar:

class CustomNavigationController: UINavigationController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     self.navigationBar.tintColor = UIColor(red:1, green:1, blue:1, alpha:0.6) 
     self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
     if #available(iOS 11.0, *) { 
      self.navigationBar.prefersLargeTitles = true 
      self.navigationItem.largeTitleDisplayMode = .automatic 
      self.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
      self.navigationBar.barTintColor = UIColor.green 
     } 
     self.navigationBar.isTranslucent = false 
     self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "navigationBarBackground"), for: .default) 
     self.navigationBar.shadowImage = #imageLiteral(resourceName: "navigationBarShadow") 
    } 
} 

奇怪的setBackgroundImage(image, for: .default)沒有爲大標題的工作。它適用於iOS 10之前,如果我旋轉iPhone(並激活小NavBar)背景又回來了嗎?

編輯: backgroundImage仍然呈現,但以某種方式隱藏。只有當您開始滾動並且出現「正常」導航欄時,backgroundImage纔可見。在這種情況下,barTintColor也完全被忽略。 screenshot GIF

+0

hi @alexkaessner。你有沒有找到解決問題的辦法..? – Tann

+0

@OceanBlue不! :/我剛剛檢查了一下新的NavBar。似乎對於顯示的大布局有一個完全不同的觀點,但這種觀點不會改變。 – alexkaessner

+0

可以設置導航欄背景顏色或bartintcolor。但不幸的是我無法設置導航欄背景圖片。這是ios 11的錯誤嗎?你知道什麼嗎..? – Tann

回答

3

在iOS中11,你不再需要設置的BackgroundImage(刪除其聲明),如果你使用大標題。相反,你需要使用BarTintColor。

class CustomNavigationController: UINavigationController { 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 
     self.navigationBar.tintColor = UIColor(red:1, green:1, blue:1, alpha:0.6) 
     self.navigationBar.titleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
     if #available(iOS 11.0, *) { 
      self.navigationBar.prefersLargeTitles = true 
      self.navigationItem.largeTitleDisplayMode = .automatic 
      self.navigationBar.largeTitleTextAttributes = [NSForegroundColorAttributeName: UIColor.white] 
      self.navigationBar.barTintColor = UIColor(red:1, green:1, blue:1, alpha:1) 
     } 
     else { 
      self.navigationBar.setBackgroundImage(#imageLiteral(resourceName: "navigationBarBackground"), for: .default)     
     } 
     self.navigationBar.shadowImage = #imageLiteral(resourceName: "navigationBarShadow") 
     self.navigationBar.isTranslucent = false 
    } 
} 
+1

感謝這篇文章,但這不是一個真正的解決方案。我也測試和分析了一點,似乎我必須堅持傳統的導航欄(現在)。 – alexkaessner

5

我通過

刪除了setBackgroundImage有同樣的問題,固定它,並使用barTint顏色與圖案像

let bgimage = imageWithGradient(startColor: UIColor.red, endColor: UIColor.yellow, size: CGSize(width: UIScreen.main.bounds.size.width, height: 1)) 
self.navigationBar.barTintColor = UIColor(patternImage: bgimage!) 

獲取圖像的漸變色

func imageWithGradient(startColor:UIColor, endColor:UIColor, size:CGSize, horizontally:Bool = true) -> UIImage? { 

    let gradientLayer = CAGradientLayer() 
    gradientLayer.frame = CGRect(x: 0, y: 0, width: size.width, height: size.height) 
    gradientLayer.colors = [startColor.cgColor, endColor.cgColor] 
    if horizontally { 
     gradientLayer.startPoint = CGPoint(x: 0.0, y: 0.5) 
     gradientLayer.endPoint = CGPoint(x: 1.0, y: 0.5) 
    } else { 
     gradientLayer.startPoint = CGPoint(x: 0.5, y: 0.0) 
     gradientLayer.endPoint = CGPoint(x: 0.5, y: 1.0) 
    } 

    UIGraphicsBeginImageContext(gradientLayer.bounds.size) 
    gradientLayer.render(in: UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
    return image 
} 
+0

我也試過這個,但我希望圖像被拉伸,而不是平鋪。我的圖像有一個漸變...或者有沒有辦法改變'patternImage:'的屬性? – alexkaessner

+0

更新了我的回答 – oldrinmendez

+0

好吧,我已經測試過了,問題依然存在。如果我旋轉iPhone,梯度太短並重復。此外'水平:假'不能正常工作。這只是一個橙色酒吧背景。 – alexkaessner

0

在Xamarin它會是這樣的:

this.NavigationBar.BackgroundColor = UIColor.Clear; 

     var gradientLayer = new CAGradientLayer 
     { 
     Frame = new CGRect(0, 0, UIApplication.SharedApplication.StatusBarFrame.Width, 
       UIApplication.SharedApplication.StatusBarFrame.Height + this.NavigationBar.Frame.Height), 
     Colors = new CGColor[] 
       {Constants.Defaults.Navigation.RealBlueColor.ToCGColor(), Constants.Defaults.Navigation.RealBlueColor.ToCGColor()} 
     }; 

     UIGraphics.BeginImageContext(gradientLayer.Bounds.Size); 
     gradientLayer.RenderInContext((UIGraphics.GetCurrentContext())); 
     UIImage image = UIGraphics.GetImageFromCurrentImageContext(); 
     UIGraphics.EndImageContext(); 

     this.View.Layer.InsertSublayer(gradientLayer, 0); 
     this.NavigationBar.BarTintColor = UIColor.FromPatternImage(image); 

this.View.Layer.Insert是可選的。我需要它,當我 「冰壺」 向上和向下導航欄

1

圖像試試這個代碼(SWIFT 4.0):

在viewDidLoad中()

self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] 
if #available(iOS 11.0, *) { 
    self.navigationController?.navigationBar.prefersLargeTitles = true 
    self.navigationItem.largeTitleDisplayMode = .automatic 
    self.navigationController?.navigationBar.largeTitleTextAttributes = [NSAttributedStringKey.foregroundColor: UIColor.black] 
} else { 
    //iOS <11.0 
} 
self.title = "Title" 
self.navigationController?.navigationBar.barTintColor = UIColor(patternImage: #imageLiteral(resourceName: "nav_bg")) 
self.navigationController?.navigationBar.isTranslucent = false 
+0

同樣的問題。對你起作用嗎? – alexkaessner

0

你需要這個;)

navigationController?.view.backgroundColor = .green 
+0

但我想添加我自己的圖像,而不僅僅是一個平坦的顏色。我在這裏使用漸變... – alexkaessner