2016-07-13 81 views
12

它与更改导航栏底部边框颜色斯威夫特

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 
    } 
} 

但是当我添加一个UITableView它不会出现在它,当我加入UISearchView出现,但删除导航栏。

任何人都知道如何解决这个问题?

回答

16

您必须调整导航栏的shadowImage属性。

试试这个。我在UIColor上创建了一个作为帮助器的类别,但是您可以按照自己喜欢的方式重构。

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 
    } 
} 

选项1:单一的导航栏上

,然后在视图控制器(改的UIColor你喜欢什么):

// 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)  

选项2:使用外观代理,上所有导航栏

而不是在每个导航栏上设置背景图像和阴影图像,可以依靠UIAppearance代理。您可以尝试将这些行添加到AppDelegate,而不是在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

谢谢,我在哪里把扩展的UIColor ? – TheoF

+0

无论你喜欢。如果你有其他的UIColor扩展名,你可以把它们放在一起,或者你可以为这个扩展名创建一个新文件,或者你可以将扩展名设置为私有的,并将它与UIViewController类一起使用。 –

+0

我在问,因为它显示错误说“声明只在文件范围有效”。 – TheoF

1

雨燕3.0只是改变这一行:

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

这样的:从@TheoF,@Alessandro和@Pavel

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

精彩的贡献。

这里是我做了...

斯威夫特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 
    } 
} 

使用它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()