2016-07-13 9 views
12

Współpracuje zZmień pasek nawigacyjny kolor dolnej granicy Swift

import UIKit 

class ViewController: UIViewController { 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 

    self.navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) 
    self.navigationController?.navigationBar.shadowImage = UIColor.redColor().as1ptImage() 


} 

override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 


} 

extension UIColor { 
    func as1ptImage() -> UIImage { 
     UIGraphicsBeginImageContext(CGSizeMake(1, 1)) 
     let ctx = UIGraphicsGetCurrentContext() 
     self.setFill() 
     CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

Ale kiedy dodać UITableView nie pojawia się na nim i kiedy dodać UISearchView wydaje ale usuwa pasek nawigacji.

Ktoś wie, jak rozwiązać ten problem?

Odpowiedz

16

Należy dostosować właściwość paska nawigacji w polu shadowImage.

Wypróbuj tę. Stworzyłem kategorię na UIColor jako pomocnika, ale możesz ją zmienić tak, jak lubisz.

extension UIColor { 
    func as1ptImage() -> UIImage { 
     UIGraphicsBeginImageContext(CGSizeMake(1, 1)) 
     let ctx = UIGraphicsGetCurrentContext() 
     self.setFill() 
     CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

Opcja 1: na jednym pasku nawigacyjnym

A następnie w kontrolerze widoku (zmiany UIColor co chcesz):

// We can use a 1px image with the color we want for the shadow image 
self.navigationController?.navigationBar.shadowImage = UIColor.redColor().as1ptImage() 

// We need to replace the navigation bar's background image as well 
// in order to make the shadowImage appear. We use the same 1px color tecnique 
self.navigationController?.navigationBar.setBackgroundImage(UIColor.yellowColor‌​().as1ptImage(), forBarMetrics: .Default)  

Opcja 2: używanie wygląd proxy, na wszystkie paski nawigacyjne

Zamiast ustawiać obraz tła i obraz cienia na każdym pasku nawigacyjnym, można polegać na proxy UIAppearance. Możesz spróbować dodać te linie do AppDelegate zamiast dodawać poprzednie do viewDidLoad.

// We can use a 1px image with the color we want for the shadow image 
UINavigationBar.appearance().shadowImage = UIColor.redColor().as1ptImage() 

// We need to replace the navigation bar's background image as well 
// in order to make the shadowImage appear. We use the same 1px color technique 
UINavigationBar.appearance().setBackgroundImage(UIColor.yellowColor().as1ptImage(), forBarMetrics: .Default) 
+0

Dzięki, gdzie mogę umieścić rozszerzenie UIColor ? – TheoF

+0

Gdzie chcesz. Jeśli masz inne rozszerzenia UIColor, możesz je połączyć, lub możesz utworzyć nowy plik tylko dla tego rozszerzenia, lub możesz uczynić rozszerzenie prywatnym i używać go razem z klasą UIViewController. –

+0

Pytałem, ponieważ pokazuje komunikat "Deklaracja jest ważna tylko w zasięgu pliku". – TheoF

1

dla Swift 3.0 tylko zmienić tę linię:

CGContextFillRect(ctx, CGRect(x: 0, y: 0, width: 1, height: 1)) 

do tego:

ctx?.fill(CGRect(x: 0, y: 0, width: 1, height: 1)) 
3

Wspaniałe wkłady @TheoF, @Alessandro i @Pavel.

Oto co zrobiłem dla ...

Swift 4

extension UIColor { 

    /// Converts this `UIColor` instance to a 1x1 `UIImage` instance and returns it. 
    /// 
    /// - Returns: `self` as a 1x1 `UIImage`. 
    func as1ptImage() -> UIImage { 
     UIGraphicsBeginImageContext(CGSize(width: 1, height: 1)) 
     setFill() 
     UIGraphicsGetCurrentContext()?.fill(CGRect(x: 0, y: 0, width: 1, height: 1)) 
     let image = UIGraphicsGetImageFromCurrentImageContext() ?? UIImage() 
     UIGraphicsEndImageContext() 
     return image 
    } 
} 

Używanie go w viewDidLoad():

/* In this example, I have a ViewController embedded in a NavigationController in IB. */ 

// Remove the background color. 
navigationController?.navigationBar.setBackgroundImage(UIColor.clear.as1ptImage(), for: .default) 

// Set the shadow color. 
navigationController?.navigationBar.shadowImage = UIColor.gray.as1ptImage() 
Powiązane problemy