将remove navigationBar border转换为swift

 舞动青春的迪斯科舞厅 发布于 2022-12-22 18:21

我正试图在swift中删除navigationBar边框.这是通过在objective-c中使用以下代码来完成的:

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

怎么能在swift中完成?

我试过这个,但没有工作:

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

小智.. 11

试试这个:

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


小智.. 8

要更改背景,文本和图标的颜色,并且还通过删除外观代理导航栏的边框/阴影,在插入此代码didFinishLaunchingWithOptions:AppDelegate:

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

注意:正如您所看到的,我们需要转换UIColorUIImage,因此您可以使用此扩展名:

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

像这样使用它: UIColor.redColor().toImage()

撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有