简体   繁体   English

将remove navigationBar border转换为swift

[英]Convert remove navigationBar border to swift

i'm trying to remove the navigationBar border in swift. 我正试图在swift中删除navigationBar边框。 This is done by using following code in objective-c: 这是通过在objective-c中使用以下代码来完成的:

[[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
[[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault]

How can this be done in swift? 怎么能在swift中完成?

i've tried this, but not working: 我试过这个,但没有工作:

UINavigationBar.appearance().shadowImage = UIImage(named: "")
UINavigationBar.appearance().setBackgroundImage(UIImage(named: ""), forBarMetrics: UIBarMetrics.Default)

Try this: 尝试这个:

UINavigationBar.appearance().shadowImage = UIImage()
UINavigationBar.appearance().setBackgroundImage(UIImage(), forBarMetrics: UIBarMetrics.Default)

To change the background, text and icons colors, and also remove the border/shadow of the navigation bar via the appearance proxy, insert this code in the didFinishLaunchingWithOptions: of AppDelegate : 要更改背景,文本和图标颜色,还要通过外观代理删除导航栏的边框/阴影,请将此代码插入AppDelegatedidFinishLaunchingWithOptions:中:

// our colors
let backgroundColor = UIColor.blueColor()
let foregroundColor = UIColor.whiteColor()

// status bar text color (.LightContent = white, .Default = black)
UIApplication.sharedApplication().statusBarStyle = .LightContent
// solid or translucent background?
UINavigationBar.appearance().translucent = false
// remove bottom shadow
UINavigationBar.appearance().shadowImage = UIImage()
// background color
UINavigationBar.appearance().setBackgroundImage(backgroundColor.toImage(), forBarMetrics: UIBarMetrics.Default)
// controls and icons color
UINavigationBar.appearance().tintColor = foregroundColor
// text color
UINavigationBar.appearance().titleTextAttributes = [NSForegroundColorAttributeName: foregroundColor]

Note: As you can see, we need to convert the UIColor to UIImage , so you can use this extension: 注意:如您所见,我们需要将UIColor转换为UIImage ,因此您可以使用此扩展:

extension UIColor{
    func toImage() -> UIImage {
        let rect = CGRectMake(0, 0, 1, 1)
        UIGraphicsBeginImageContextWithOptions(rect.size, true, 0)
        self.setFill()
        UIRectFill(rect)
        var image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
}

Use it like this: UIColor.redColor().toImage() 像这样使用它: UIColor.redColor().toImage()

我使用以下代码从应用程序中删除导航栏阴影。

    self.navigationController?.navigationBar.clipsToBounds = true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM