【问题标题】:Convert remove navigationBar border to swift将删除导航栏边框转换为 swift
【发布时间】:2014-11-03 06:27:13
【问题描述】:

我正在尝试快速删除导航栏边框。这是通过在objective-c中使用以下代码来完成的:

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

如何快速做到这一点?

我试过了,但是不行:

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

【问题讨论】:

  • 没有错误信息

标签: ios objective-c iphone swift uinavigationcontroller


【解决方案1】:

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

    self.navigationController?.navigationBar.clipsToBounds = true

【讨论】:

  • 是的,如果您不怕丢失状态栏的背景颜色,则可以使用
  • 它工作正常,这是正确的答案,我有状态栏的自定义颜色,并且没有松动它。至少这适用于自定义导航栏。我不使用navigationController?.navigationBar,而是将自己添加到情节提要中的UINavigationBar。因为我的导航栏需要一些特定的自定义。谢谢@Rahul Nair!
【解决方案2】:

试试这个:

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

【讨论】:

    【解决方案3】:

    要更改背景、文本和图标颜色,以及通过外观代理删除导航栏的边框/阴影,请将此代码插入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]
    

    注意:如您所见,我们需要将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
        }
    }
    

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

    【讨论】:

      猜你喜欢
      • 2018-10-15
      • 2014-12-10
      • 1970-01-01
      • 2020-03-19
      • 1970-01-01
      • 2016-07-02
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      相关资源
      最近更新 更多